VOOZH about

URL: https://www.geeksforgeeks.org/dsa/fizz-buzz-implementation/

⇱ Fizz Buzz - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Fizz Buzz

Last Updated : 23 Jul, 2025

Given an integer n, for every positive integer i <= n, the task is to print,

  • "FizzBuzz" if i is divisible by 3 and 5,
  • "Fizz" if i is divisible by 3,
  • "Buzz" if i is divisible by 5
  • "i" as a string, if none of the 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"]

[Naive Approach] By checking every integer individually

A very simple approach to solve this problem is that we can start checking each number from 1 to n to see if it's divisible by 3, 5, or both. Depending on the divisibility:

  • If a number is divisible by both 3 and 5, append "FizzBuzz" into result.
  • If it's only divisible by 3, append "Fizz" into result.
  • If it's only divisible by 5, append "Buzz" into result.
  • Otherwise, append the number itself into result.

Output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz 

Time Complexity: O(n), since we need to traverse the numbers from 1 to n in any condition.
Auxiliary Space: O(n), for storing the result

[Better Approach] By String Concatenation

While the naive approach works well for the basic FizzBuzz problem, it becomes very complicated if additional mappings comes under picture, such as "Jazz" for multiples of 7. The number of conditions for checking would increases.

Instead of checking every possible combination of divisors, we check each divisor separately and concatenate the corresponding strings if the number is divisible by them.

Step-by-step approach:

  • For each number i <= n, start with an empty string s.
  • Check if i is divisible by 3, append "Fizz" into s.
  • Check if i is divisible by 5, append "Buzz" into s.
  • If we add "Fizz" and "Buzz", the string s becomes "FizzBuzz" and we don't need extra comparisons to check divisibility of both.
  • If nothing was added, just use the number.
  • Finally, append this string s into our result array.

Output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz 

Time Complexity: O(n), since we need to traverse the numbers from 1 to n in any condition.
Auxiliary Space: O(n), for storing the result

[Expected Approach] Using Hash Map or Dictionary

When we have many words to add like "Fizz", "Buzz", "Jazz" and more, the second method can still get complicated and lengthy. To make things cleaner, we can use something called a hash map. Initially, we can store the divisors and their corresponding words into hash map.

For this problem, we would map 3 to "Fizz" and 5 to "Buzz" and for each number, checks if it is divisible by 3 or 5, if so, appends the corresponding string from map to the result. If the number is not divisible by either, simply adds the number itself as a string.


Output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz 

Time Complexity: O(n), since we need to traverse the numbers from 1 to n in any condition.
Auxiliary Space: O(n), for storing the result


Comment
Article Tags:
Article Tags: