--- Difficulty: Easy Source: 160 Days of Problem Solving (BONUS PROBLEMS 🎁) Tags: - Strings - Modulus - Arrays --- # πŸš€ _1. Fizz Buzz_ 🧠 The problem can be found at the following link: [Problem Link](https://www.geeksforgeeks.org/batch/gfg-160-problems/track/string-bonus-problems/problem/fizz-buzz) ## πŸ’‘ **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:** ``` n = 20 ``` **Output:** ``` ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz", "16", "17", "Fizz", "19", "Buzz"] ``` ### 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) ```cpp class Solution { public: vector fizzBuzz(int n) { vector result; result.reserve(n); for (int i = 1; i <= n; i++) { string current = ""; if (i % 3 == 0) current += "Fizz"; if (i % 5 == 0) current += "Buzz"; if (current.empty()) current = to_string(i); result.push_back(current); } return result; } }; ``` ## Code (Java) ```java class Solution { public static ArrayList fizzBuzz(int n) { ArrayList result = new ArrayList<>(n); for (int i = 1; i <= n; i++) { StringBuilder current = new StringBuilder(); if (i % 3 == 0) current.append("Fizz"); if (i % 5 == 0) current.append("Buzz"); if (current.length() == 0) current.append(i); result.add(current.toString()); } return result; } } ``` ## Code (Python) ```python class Solution: def fizzBuzz(self, n: int): result = [] for i in range(1, n + 1): current = [] if i % 3 == 0: current.append("Fizz") if i % 5 == 0: current.append("Buzz") if not current: current.append(str(i)) result.append("".join(current)) return result ``` ## 🎯 Contribution and Support: For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: [Any Questions](https://www.linkedin.com/in/patel-hetkumar-sandipbhai-8b110525a/). Let’s make this learning journey more collaborative! ⭐ If you find this helpful, please give this repository a star! ⭐ ---

πŸ“Visitor Count