Fizz Buzz Leetcode

Difficulty Level Easy
Frequently asked in Bloomberg Microsoft
algorithms coding Interview interviewprep LeetCode LeetCodeSolutions StringViews 5123

In Fizz Buzz problem we have given a number n, print the string representation of numbers from 1 to n with the given conditions:

  1. Print “Fizz” for multiples of 3.
  2. Print “Buzz” for multiples of 5.
  3. Print “FizzBuzz” for multiples of both 3 and 5.
  4. Otherwise, print the number in string format.

Example

Input:

n=4

Output:

1

2

Fizz

4

Algorithm for Fizz Buzz

  1. Iterate on the numbers from 1 to n( loop variable is i).
  2. For every number, if it is divisible by both 3 and 5 i.e., i%3=0 and i%5=0, then print “FizzBuzz”.
  3. Else, if the number is divisible by 3 i.e., i%3=0, then print “Fizz”.
  4. Else, if the number is divisible by 5 i.e., i%5=0, print “Buzz”.
  5. Else, print the number as a string.

Implementation

C++ Program for Fizz Buzz Leetcode

#include<bits/stdc++.h>
using namespace std;
void fizzbuzz(int n){
    for(int i=1;i<=n;i++){
        if(i%3 == 0 && i%5==0){
            cout<<"Fizzbuzz\n";
        }
        else if(i%3 == 0){
            cout<<"Fizz\n";
        }
        else if(i%5 == 0){
            cout<<"Buzz\n";
        }
        else{
            cout<<to_string(i)<<"\n";
        }
    }
}
int main(){
    int n;
    cin>>n;
    fizzbuzz(n);
}
14
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14

JAVA Program for Fizz Buzz Leetcode

import java.util.Scanner;
class Main { 
    static void fizzbuzz(int n){
        for(int i=1;i<=n;i++){
            if(i%3 == 0 && i%5==0){
                System.out.println("Fizzbuzz");
            }
            else if(i%3 == 0){
                System.out.println("Fizz");
            }
            else if(i%5 == 0){
                System.out.println("Buzz");
            }
            else{
                System.out.println(Integer.toString(i));
            }
        }
    }
  
    public static void main(String args[]) 
    { 
        int n;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        fizzbuzz(n);
    } 
}
7
1
2
Fizz
4
Buzz
Fizz
7

Variations

Write “Fizz” for the number divisible by 4 and “Buzz” for number divisible by 8, print any valid answer. In this case, numbers that are divisible by 8 are also divisible by 4 as 8 is a multiple of 4. Hence there may be multiple valid answers for this question as on multiple of 8 we can either choose “Fizz” or “Buzz”. Even if you observe carefully then we can replace all the numbers which divisible by 4 with “Fizz”, that will also be a valid answer.

Note: Carefully observe while solving this question as we saw that if the given numbers are multiple of each other then we can have multiple valid answers.

Complexity Analysis

Time complexity

O(n) where n is the number given to us. As we need to traverse every number once from 1 to N for print its a string format.

Space Complexity

O(1) because we don’t use or create any extra auxiliary space.

References

Translate »