Factorial Trailing Zeroes - Solution & Explanation
Problem Statement
Given an integer n, return the number of trailing zeroes in n!.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Example 1:
Input: n = 3 Output: 0 Explanation: 3! = 6, no trailing zero.
Example 2:
Input: n = 5 Output: 1 Explanation: 5! = 120, one trailing zero.
Example 3:
Input: n = 0 Output: 0
Constraints:
0 <= n <= 104
Follow up: Could you write a solution that works in logarithmic time complexity?
Approach Overview
Problem Overview: Given an integer n, return the number of trailing zeroes in n! (n factorial). Trailing zeroes appear when the factorial result contains factors of 10, which are formed by multiplying 2 Γ 5.
The key observation: factorials contain far more factors of 2 than 5. That means the number of trailing zeroes is determined entirely by how many times 5 appears in the prime factorization of numbers from 1 to n. This turns the problem into a counting exercise rather than computing the factorial itself.
Approach 1: Iterative Counting (O(n log n) time, O(1) space)
Iterate through every number from 1 to n. For each number, repeatedly divide by 5 while it remains divisible. Every successful division contributes one factor of 5. Accumulate these counts across the entire range. For example, 25 contributes two factors because 25 = 5 Γ 5, and 125 contributes three.
This approach directly counts how many times 5 appears in the prime factorization of each number. It avoids computing the factorial but still checks every value individually. Time complexity is O(n log n) in the worst case due to repeated division, and space complexity is O(1). Itβs useful for understanding the mechanics behind trailing zero formation but not optimal for large inputs.
Approach 2: Counting Factors of 5 (O(log n) time, O(1) space)
Instead of examining every number, count how many multiples of 5, 25, 125, and higher powers exist up to n. Each multiple contributes at least one factor of 5. Numbers divisible by higher powers contribute additional factors. The count can be computed using:
n/5 + n/25 + n/125 + ...
Each division counts how many numbers contribute that many factors of 5. Continue dividing by 5 until the divisor exceeds n. The number of iterations grows logarithmically with base 5, giving O(log n) time complexity and O(1) space.
This technique relies purely on mathematical insight rather than iteration over all values. It appears frequently in math and number theory interview problems where counting prime factors leads to an efficient solution.
Recommended for interviews: The counting factors of 5 method is the expected solution. Interviewers want to see that you recognize trailing zeroes come from 10 = 2 Γ 5 and that 5 is the limiting factor in factorials. Explaining the iterative counting approach first demonstrates understanding, but deriving the logarithmic counting formula shows stronger algorithmic insight.
Approach 1: Counting Factors of 5
To determine the number of trailing zeroes in a factorial (n!), observe that trailing zeroes are produced by 10s in the factorization of n!. Each 10 is the product of a 2 and a 5. In most factorial numbers, there are more factors of 2 than 5. Therefore, the number of trailing zeroes is determined by the number of times 5 is a factor. We count the number of multiples of 5 in the numbers from 1 to n (because each contributes at least one factor of 5). Then, we count the multiples of 25, 125, 625, etc., as these contribute an extra factor of 5 each time. Continue this counting until the power of 5 exceeds n.
The function trailingZeroes calculates the number of trailing zeroes in the factorial of a given number n. It repeatedly divides n by 5, adding the quotient to count, which represents the number of trailing zeroes.
Complexity
Time Complexity: O(log5 n)
Space Complexity: O(1)
Approach 2: Iterative Counting
In this approach, we directly count the number of trailing zeroes by iterating over all the numbers up to n, checking if each is divisible by 5. If so, we increment a counter to track the factors of 5. This approach isn't as efficient as counting the most significant power of 5 due to potentially higher iteration counts.
This C implementation iterates over numbers from 1 to n. For each number, if it is divisible by 5, it divides it further, counting each division as a factor of 5.
Complexity
Time Complexity: O(n log5 n)
Space Complexity: O(1)
Approach 3: Mathematics
The problem is actually asking how many factors of 5 are there in [1,n].
Let's take 130 as an example for analysis:
- Divide by
5for the first time, get26, indicating that there are26numbers containing the factor5; - Divide by
5for the second time, get5, indicating that there are5numbers containing the factor5^2; - Divide by
5for the third time, get1, indicating that there is1number containing the factor5^3; - Sum up to get the count of all factors of
5in[1,n].
The time complexity is O(log n), and the space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Counting Factors of 5 | Time Complexity: O(log5 n) |
| Iterative Counting | Time Complexity: O(n log5 n) |
| Mathematics | β |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Counting | O(n log n) | O(1) | When explaining how factors of 5 appear in numbers from 1..n or demonstrating the concept step by step. |
| Counting Factors of 5 | O(log n) | O(1) | Optimal solution for interviews and large inputs; avoids iterating through all numbers. |
Video Solution
Factorial Trailing Zeroes | LeetCode 172 | Coding Interview Tutorial β’ Terrible Whiteboard β’ 9,877 views views
Watch 9 more video solutions βFrequently Asked Questions
Is Factorial Trailing Zeroes easy or hard?
Factorial Trailing Zeroes Python/Java solution
How to solve Factorial Trailing Zeroes in O(n)?
What is the best approach for Factorial Trailing Zeroes?
Is Factorial Trailing Zeroes asked at Google/Amazon/Meta?
What data structure is used in Factorial Trailing Zeroes?
What is the time complexity of Factorial Trailing Zeroes?
Ready to solve this problem?
Practice Factorial Trailing Zeroes with our built-in code editor and test cases.
Practice on FleetCode