πŸš€1. Fizz Buzz 🧠

The problem can be found at the following link: Problem Link

πŸ’‘ Problem Description:

Given an integer n, for every integer 1 ≀ i ≀ n, the task is to return an array of strings where:

  • "FizzBuzz" is included if i is divisible by both 3 and 5,

  • "Fizz" is included if i is divisible only by 3,

  • "Buzz" is included if i is divisible only by 5,

  • The number i itself is included as a string if none of the above conditions are true.

Examples:

Input:

n = 3

Output:

["1", "2", "Fizz"]

Input:

n = 10

Output:

["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"]

Input:

Output:

Constraints:

  • $1 ≀ n ≀ 10^6$

🎯 My Approach:

Step-by-Step:

  1. Iterate from 1 to n: Loop through all integers from 1 to n.

  2. Check divisibility:

    • Use modulus (%) to check if i is divisible by both 3 and 5, only by 3, or only by 5.

    • If divisible by both 3 and 5, append "FizzBuzz".

    • If divisible only by 3, append "Fizz".

    • If divisible only by 5, append "Buzz".

    • Otherwise, append the number itself as a string.

  3. Store Results: Add the computed value to the result list or array.

  4. Output Results: Return the result array.

πŸ•’ Time and Auxiliary Space Complexity

  • Time Complexity: O(n) The algorithm iterates once through all integers from 1 to n. Each iteration involves constant-time operations to check divisibility and string concatenation.

  • Space Complexity: O(n) The result array stores all n strings.

πŸ“ Solution Code

Code (Cpp)

Code (Java)

Code (Python)

🎯 Contribution and Support:

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Last updated