π1. Fizz Buzz π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
Given an integer n, for every integer 1 β€ i β€ n, the task is to return an array of strings where:
"FizzBuzz" is included if
iis divisible by both 3 and 5,"Fizz" is included if
iis divisible only by 3,"Buzz" is included if
iis divisible only by 5,The number
iitself is included as a string if none of the above conditions are true.
Examples:
Input:
n = 3Output:
["1", "2", "Fizz"]Input:
n = 10Output:
["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"]Input:
Output:
Constraints:
$
1 β€ n β€ 10^6$
π― My Approach:
Step-by-Step:
Iterate from 1 to
n: Loop through all integers from 1 ton.Check divisibility:
Use modulus (
%) to check ifiis divisible by both 3 and 5, only by 3, or only by 5.If divisible by both 3 and 5, append
"FizzBuzz".If divisible only by 3, append
"Fizz".If divisible only by 5, append
"Buzz".Otherwise, append the number itself as a string.
Store Results: Add the computed value to the result list or array.
Output Results: Return the result array.
π Time and Auxiliary Space Complexity
Time Complexity: O(n) The algorithm iterates once through all integers from 1 to
n. Each iteration involves constant-time operations to check divisibility and string concatenation.Space Complexity: O(n) The result array stores all
nstrings.
π Solution Code
Code (Cpp)
Code (Java)
Code (Python)
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated