Factorial Trailing Zeroes - Problem

Given an integer n, return the number of trailing zeroes in n!.

Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.

Follow up: Could you write a solution that works in logarithmic time complexity?

Input & Output

Example 1 — Small Factorial
$ Input: n = 3
Output: 0
💡 Note: 3! = 6, which has no trailing zeros
Example 2 — One Trailing Zero
$ Input: n = 5
Output: 1
💡 Note: 5! = 120, which has 1 trailing zero (from 2×5=10)
Example 3 — Multiple Zeros
$ Input: n = 25
Output: 6
💡 Note: 25! has 6 trailing zeros from factors: 5 multiples of 5 plus 1 extra from 25=5×5

Constraints

  • 0 ≤ n ≤ 104

Visualization

Tap to expand
Factorial Trailing Zeroes: How 10s are Formedn=5Input5! = 1×2×3×4×52factor5factor2 × 5 = 10 → trailing zero1205! = 120 (1 trailing zero)Output: 1
Understanding the Visualization
1
Input
Integer n representing factorial
2
Process
Count factors of 5 in n!
3
Output
Number of trailing zeros
Key Takeaway
🎯 Key Insight: Trailing zeros come from pairs of factors 2 and 5, with 5s being the limiting factor
Asked in
Microsoft 35 Amazon 28 Apple 22 Google 18
85.0K Views
Medium Frequency
~15 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen