Preimage Size of Factorial Zeroes Function - Problem

Let f(x) be the number of zeroes at the end of x!. Recall that x! = 1 * 2 * 3 * ... * x and by convention, 0! = 1.

For example, f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has two zeroes at the end.

Given an integer k, return the number of non-negative integers x have the property that f(x) = k.

Input & Output

Example 1 — Basic Case
$ Input: k = 3
Output: 5
💡 Note: f(15) = f(16) = f(17) = f(18) = f(19) = 3, so exactly 5 numbers give 3 trailing zeros
Example 2 — Special Case Zero
$ Input: k = 0
Output: 1
💡 Note: Only f(0) = 0, since 0! = 1 has no trailing zeros. All other factorials have trailing zeros.
Example 3 — Impossible Value
$ Input: k = 4
Output: 0
💡 Note: No factorial has exactly 4 trailing zeros. The count jumps from 3 to 7, skipping 4,5,6.

Constraints

  • 0 ≤ k ≤ 109

Visualization

Tap to expand
Factorial Trailing Zeros Patternf=0f=1f=2f=3f=4f=6f=71 num5 nums5 nums5 nums0 nums0 nums5 numsPattern: f(x) exists → 1 or 5 numbers, f(x) missing → 0 numbersExample: k=3 has 5 solutions, k=4 has 0 solutionsKey: Use binary search to find if k is possible
Understanding the Visualization
1
Input k
Target number of trailing zeros
2
Search Pattern
f(x) increases but skips values
3
Count Results
Return 0, 1, or 5
Key Takeaway
🎯 Key Insight: Factorial trailing zeros have gaps - some counts are impossible, others appear exactly 5 times consecutively!
Asked in
Google 15 Microsoft 8
12.0K Views
Medium Frequency
~25 min Avg. Time
450 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