Problem 5: FizzBuzz | Array 1 Dimensional
Problem Description
Given a positive integer A, return an array of strings with all the integers from 1 to N. But for multiples of 3 the array should have “Fizz” instead of the number. For the multiples of 5, the array should have “Buzz” instead of the number. For numbers which are multiple of 3 and 5 both, the array should have “FizzBuzz” instead of the number.
Look at the example for more details.
Problem Constraints
Input Format
The first argument has the integer A.
Output Format
Return an array of string.
Example Input
Input 1:
A = 5
Example Output
Output 1:
[1 2 Fizz 4 Buzz]
Problem Description
Given a positive integer A, return an array of strings with all the integers from 1 to N. But for multiples of 3 the array should have “Fizz” instead of the number. For the multiples of 5, the array should have “Buzz” instead of the number. For numbers which are multiple of 3 and 5 both, the array should have “FizzBuzz” instead of the number.
Look at the example for more details.
Example Explanation
Explanation 1:
3 is divisible by 3 so it is replaced by “Fizz”. Similarly, 5 is divisible by 5 so it is replaced by “Buzz”.
public class Solution {
public ArrayList<String> fizzBuzz(int A) {
ArrayList<String> output = new ArrayList<>();
for (int currentNumber = 1; currentNumber <= A; ++currentNumber ) {
if (currentNumber % 15 == 0) {
output.add("FizzBuzz");
}
else if (currentNumber % 5 == 0) {
output.add("Buzz");
}
else if (currentNumber % 3 == 0) {
output.add("Fizz");
}
else {
output.add(Integer.toString( currentNumber ) );
}
}
return output;
}
}