Given an integer n
, return the number of trailing zeroes in n!
. Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1
.
For example:
n = 3
, the output should be 0
. Explanation: 3! = 6
, which has no trailing zeros.n = 5
, the output should be 1
. Explanation: 5! = 120
, which has one trailing zero.n = 0
, the output should be 0
.Write an algorithm with the best possible time complexity. What are the edge cases to consider?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks us to figure out how many trailing zeros are at the end of the factorial of a number. A brute force approach directly calculates the factorial and then counts the trailing zeros by repeatedly dividing by ten.
Here's how the algorithm would work step-by-step:
def factorial_trailing_zeroes_brute_force(number):
factorial_result = 1
# Calculate the factorial of the given number.
for current_number in range(1, number + 1):
factorial_result *= current_number
trailing_zero_count = 0
# Count the trailing zeros by repeatedly dividing by 10.
while factorial_result > 0:
if factorial_result % 10 == 0:
factorial_result //= 10
trailing_zero_count += 1
# This is the core logic to identify and count trailing zeros
else:
break
return trailing_zero_count
The key is to realize trailing zeroes in factorials come from factors of 5. Instead of calculating the factorial, we directly count how many times 5 appears as a factor in the numbers leading up to the given number. This is much faster than calculating the factorial and then counting zeroes.
Here's how the algorithm would work step-by-step:
def factorial_trailing_zeroes(number):
trailing_zeroes_count = 0
power_of_five = 5
# Count multiples of 5, 25, 125, etc.
while number // power_of_five > 0:
# Accumulate count of factors of 5.
trailing_zeroes_count += number // power_of_five
power_of_five *= 5
# Return the total count of trailing zeroes.
return trailing_zeroes_count
Case | How to Handle |
---|---|
Negative input n | Factorial is not defined for negative numbers, return 0 as there are no trailing zeroes. |
Input n equals 0 | 0! is 1, which has no trailing zeroes, so return 0. |
Input n equals 1 | 1! is 1, which has no trailing zeroes, so return 0. |
Large input n, close to integer limit | Use long data type to prevent integer overflow during calculations with large n or optimize for time complexity using only division and counting factors of 5. |
Input n is a multiple of 5, 25, 125 (powers of 5) | The algorithm should correctly count multiple factors of 5 (e.g., 25 contributes two factors of 5). |
Integer overflow when calculating factorial directly (if attempted naively) | Avoid calculating the factorial directly; instead, count the factors of 5. |
Extremely large n that may cause performance degradation | Ensure the solution has logarithmic or constant time complexity related to the range, avoiding iterating through all numbers up to n. |
Input close to max integer value | Be sure to prevent overflow when dividing to count the multiples of 5. |