The problem name might seem fuzzy. Fizz Buzz is a game with which children are taught about the division. So, without much hassle let’s clear the buzz around it.
Problem Statement
Let us write a program where for multiples of 3 you print “Fizz”, for the multiples of 5 “Buzz” and FizzBuzz for multiples of both.
This is a basic problem and more like it can be looked at.
Table of Contents
Approach-1
Brute Force
- Given a number n
- Run a loop from 1 to n
- If a number is divisible by 15.Print FizzBuzz
- Else If a number is divisible by 3.Print Fizz
- Else a number is divisible by 3.Print Buzz
Voila, we have with us the code for FizzBuzz! Let us look into the code.
Java Code For Fizz Buzz
class Solution { public List<String> fizzBuzz(int n) { List<String>lisa=new ArrayList<String>(); for(int i=1;i<n+1;i++) { if(i%15==0) lisa.add("FizzBuzz"); else if(i%5==0) lisa.add("Buzz"); else if(i%3==0) lisa.add("Fizz"); else lisa.add(Integer.toString(i)); } return lisa; } }
C++ Code For Fizz Buzz
#include<string> class Solution { public: vector<string> fizzBuzz(int n) { vector<string>lisa; for(int i=1;i<n+1;i++) { if(i%15==0) lisa.push_back("FizzBuzz"); else if(i%5==0) lisa.push_back("Buzz"); else if(i%3==0) lisa.push_back("Fizz"); else lisa.push_back(to_string(i)); } return lisa; } };
Complexity Analysis
Time complexity = O(n) where n is the number till we have to print the values in the fizz buzz way.
Space Complexity = O(1) because we don’t use any auxiliary space here.
Approach-2
Concatenating Strings
This method will not help with the runtime but definitely help with cutting on the lines of code and with problems with extra conditions.
The reader may question me now. What extra conditions?
Let us assume we change the problem from FizzBuzz to FizzBuzzLizz.
- Print Fizz every time you encounter a multiple of 3
- Output Buzz every time we see a multiple of 5
- Print Lizz with each occurrence of 7’s multiples
- Add FizzBuzz for multiples of both 3 and 5
- FizzLizz for multiples of both 3 and 7
- Print BuzzLizz when we encounter multiples of both 5 and 7
Does it not lead to way too many if and else’s?
Any way to escape it.YES!
We take a string every iteration and keep adding to it.
- Run a loop from 1 to n.
- We store the answer in a string ans=” “;
- For each i
- Add Fizz to the string if the number is multiple of 3
- Concatenate Buzz if the number is multiple of 5
- Sum up Lizz to the string if the number is multiple of 7
- Thus creating FizzBuzz Lizz if the number is a multiple of all 3,5 and 7
- Throw in the string formed to an ArrayList or any other data structure you have used
Better right? Let’s look into the code:
Java Code For Fizz Buzz
class Solution { public List<String> fizzBuzz(int n) { List<String>lisa=new ArrayList<String>(); for(int i=1;i<n+1;i++) { String ans=""; if(i%5==0) ans=ans+"Buzz"; if(i%3==0) ans=ans+"Fizz"; else ans=ans+Integer.toString(i); lisa.add(ans); } return lisa; } }
C++ Code For Fizz Buzz
#include<string> class Solution { public: vector<string> fizzBuzz(int n) { vector<string>lisa; for(int i=1;i<n+1;i++) { string ans=""; if(i%5==0) ans=ans+"Buzz"; if(i%3==0) ans=ans+"Fizz"; else ans=ans+to_string(i); lisa.push_back(ans); } return lisa; } };
Complexity Analysis
Time Complexity Of the Above Solution: O(n) where n is the number till we have to print the values in the fizz buzz way.
Time Complexity Of the Above Solution: