2. FizzBuzz

Given an integer n, return a string array answer (1-indexed) where: answer[i] == "FizzBuzz" if i is divisible by 3 and 5, answer[i] == "Fizz" if i is divisible by 3, answer[i] == "Buzz" if i is divisible by 5, answer[i] == i (as a string) if none of the above conditions are true.

Examples:

Input: n = 3
Output: ["1", "2", "Fizz"]

Input: n = 5
Output: ["1", "2", "Fizz", "4", "Buzz"]

Input: n = 15
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]

Problem Breakdown:

  1. We create an empty result array to store our answers. We will iterate from 1 to n (inclusive) and determine what to add for each number.
    • result = []
  2. We loop from 1 to n inclusive. For each number i, we need to check divisibility conditions in the correct order.
    • for i in range(1, n + 1):
  3. First, we check if i is divisible by BOTH 3 and 5 (i.e., divisible by 15). This check MUST come first because if we check for 3 or 5 individually first, we would never reach the FizzBuzz case.
    • if i % 3 == 0 and i % 5 == 0:
          result.append("FizzBuzz")
  4. Next, we check if i is divisible by 3 only. If so, we append "Fizz".
    • elif i % 3 == 0:
          result.append("Fizz")
  5. Then, we check if i is divisible by 5 only. If so, we append "Buzz".
    • elif i % 5 == 0:
          result.append("Buzz")
  6. If none of the above conditions are met, the number is not divisible by 3 or 5, so we append the number itself as a string.
    • else:
          result.append(str(i))

Summary:

Iterate from 1 to n. For each number, check divisibility by both 3 and 5 first (FizzBuzz), then by 3 (Fizz), then by 5 (Buzz). If none apply, use the number itself. The key insight is checking the combined condition first to avoid it being caught by individual checks.

Time and Space Complexity:

Time Complexity: O(n) - we iterate from 1 to n once.

Space Complexity: O(n) - we store n strings in the result array.

Python Solution:

def fizzBuzz(n):
    result = []
    for i in range(1, n + 1):
        if i % 3 == 0 and i % 5 == 0:
            result.append("FizzBuzz")
        elif i % 3 == 0:
            result.append("Fizz")
        elif i % 5 == 0:
            result.append("Buzz")
        else:
            result.append(str(i))
    return result

JavaScript Solution:

var fizzBuzz = function(n) {
    const result = [];
    for (let i = 1; i <= n; i++) {
        if (i % 3 === 0 && i % 5 === 0) {
            result.push("FizzBuzz");
        } else if (i % 3 === 0) {
            result.push("Fizz");
        } else if (i % 5 === 0) {
            result.push("Buzz");
        } else {
            result.push(String(i));
        }
    }
    return result;
};

Java Solution:

class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> result = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                result.add("FizzBuzz");
            } else if (i % 3 == 0) {
                result.add("Fizz");
            } else if (i % 5 == 0) {
                result.add("Buzz");
            } else {
                result.add(String.valueOf(i));
            }
        }
        return result;
    }
}