Problem Description
Given an integer k, let f(x) be the number of trailing zeroes in x! (the factorial of x). The task is to determine how many non-negative integers x satisfy f(x) = k. For example, when k = 0, x can be 0, 1, 2, 3, or 4 since none of these factorials end with any zeroes.
Key Insights
- The trailing zeros in x! are determined by the number of times 5 (and its powers) divide x!, as 2s are abundant.
- f(x) can be computed by summing floor(x/5) + floor(x/25) + floor(x/125) + … until the division results in zero.
- f(x) is a monotonic (non-decreasing) function, making binary search applicable.
- Use binary search twice to determine the leftmost (first) and rightmost (last) x that satisfy f(x) = k. The count of valid x values is the size of this interval.
- If no x exists such that f(x) equals k, then the result is 0.
Space and Time Complexity
Time Complexity: O(log(n)) per binary search call (n is chosen as 5*(k+1)), so overall O(log(n)). Space Complexity: O(1).
Solution
We solve the problem by first defining a helper function that calculates the number of trailing zeros in x!. Then, we use binary search to find the smallest x that yields exactly k trailing zeroes and likewise to find the largest x. The number of integers satisfying the condition is given by (right_boundary - left_boundary + 1). If no x meets the condition, we return 0. This approach exploits the monotonic nature of the trailing zero function for efficient searching.