809. Preimage Size of Factorial Zeroes Function
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the number of non-negative integers x such that f(x) = k, where f(x) is the number of trailing zeros in x!.
Solution
Preimage Size of Factorial Zeroes Function in Python
class Solution:
def preimageSizeFZF(self, k: int) -> int:
# The number of x's such that f(x) = k is g(k+1) - g(k),
# where g(v) is the smallest x such that f(x) >= v.
# This difference is always 0 or 5.
return self.smallest_x_with_at_least_k_zeros(k + 1) - self.smallest_x_with_at_least_k_zeros(k)
def smallest_x_with_at_least_k_zeros(self, k_target: int) -> int:
"""Helper function to find the smallest x such that f(x) >= k."""
if k_target == 0:
return 0
low, high = 0, 5 * k_target
ans = high + 1
while low <= high:
mid = (low + high) // 2
if self.count_trailing_zeros(mid) >= k_target:
ans = mid
high = mid - 1
else:
low = mid + 1
return ans
def count_trailing_zeros(self, x: int) -> int:
"""Helper function to calculate f(x), the number of trailing zeros in x!."""
if x < 0:
return 0
count = 0
# The number of trailing zeros is the number of factors of 5.
# which is floor(x/5) + floor(x/25) + floor(x/125) + ...
power_of_5 = 5
while power_of_5 <= x:
count += x // power_of_5
power_of_5 *= 5
return count
Preimage Size of Factorial Zeroes Function in Java
class Solution {
public int preimageSizeFZF(int k) {
// The number of x's such that f(x) = k is g(k+1) - g(k),
// where g(v) is the smallest x such that f(x) >= v.
// This difference is always 0 or 5.
return (int)(smallestXwithAtLeastKZeros((long)k + 1) - smallestXwithAtLeastKZeros(k));
}
// Helper function to find the smallest x such that f(x) >= k.
// This is solved using binary search.
private long smallestXwithAtLeastKZeros(long k_target) {
if (k_target == 0) return 0;
long low = 0;
// A safe upper bound for x is 5*k.
// f(x) approx x/4. If f(x) = k, x approx 4k.
// f(5k) = k + f(k) > k. So solution is < 5k.
long high = 5L * k_target;
long ans = high + 1; // Initialize ans to a value >= any possible answer
while (low <= high) {
long mid = low + (high - low) / 2;
if (countTrailingZeros(mid) >= k_target) {
ans = mid; // Found a potential answer, try for a smaller x
high = mid - 1;
} else {
low = mid + 1; // mid is too small, need larger x
}
}
return ans;
}
// Helper function to calculate f(x), the number of trailing zeros in x!
private long countTrailingZeros(long x) {
if (x < 0) return 0;
long count = 0;
// The number of trailing zeros is the number of factors of 5.
// which is floor(x/5) + floor(x/25) + floor(x/125) + ...
while (x > 0) {
count += x / 5;
x /= 5;
}
return count;
}
}
Preimage Size of Factorial Zeroes Function in C++
class Solution {
public:
int preimageSizeFZF(int k) {
// The number of x's such that f(x) = k is g(k+1) - g(k),
// where g(v) is the smallest x such that f(x) >= v.
// The difference is always 0 or 5.
return static_cast<int>(smallestXwithAtLeastKZeros(k + 1) - smallestXwithAtLeastKZeros(k));
}
private:
// Helper function to calculate f(x), the number of trailing zeros in x!
long long countTrailingZeros(long long x) {
if (x < 0) return 0;
long long count = 0;
// The number of trailing zeros is the number of factors of 5.
// which is floor(x/5) + floor(x/25) + floor(x/125) + ...
while (x > 0) {
count += x / 5;
x /= 5;
}
return count;
}
// Helper function to find the smallest x such that f(x) >= k.
// This is solved using binary search.
long long smallestXwithAtLeastKZeros(long long k_target) {
if (k_target == 0) return 0;
long long low = 0;
// A safe upper bound for x is 5*k. Let's be generous.
// f(x) approx x/4. If f(x) = k, x approx 4k.
// f(5k) = k + f(k) > k. So solution is < 5k.
long long high = 5LL * k_target;
long long ans = high + 1; // Initialize ans to a value >= any possible answer
while (low <= high) {
long long mid = low + (high - low) / 2;
if (countTrailingZeros(mid) >= k_target) {
ans = mid; // Found a potential answer, try for a smaller x
high = mid - 1;
} else {
low = mid + 1; // mid is too small, need larger x
}
}
return ans;
}
};
Preimage Size of Factorial Zeroes Function in TypeScript
function preimageSizeFZF(k: number): number {
// The number of x's such that f(x) = k is g(k+1) - g(k),
// where g(v) is the smallest x such that f(x) >= v.
// This difference is always 0 or 5.
return smallestXwithAtLeastKZeros(k + 1) - smallestXwithAtLeastKZeros(k);
};
// Helper function to calculate f(x), the number of trailing zeros in x!
function countTrailingZeros(x: number): number {
if (x < 0) return 0;
let count = 0;
// The number of trailing zeros is the number of factors of 5.
// which is floor(x/5) + floor(x/25) + floor(x/125) + ...
let tempX = x;
while (tempX > 0) {
tempX = Math.floor(tempX / 5);
count += tempX;
}
return count;
}
// Helper function to find the smallest x such that f(x) >= k.
// This is solved using binary search.
function smallestXwithAtLeastKZeros(k_target: number): number {
if (k_target === 0) return 0;
let low = 0;
// A safe upper bound for x is 5*k.
let high = 5 * k_target;
let ans = high + 1;
while (low <= high) {
let mid = Math.floor(low + (high - low) / 2);
if (countTrailingZeros(mid) >= k_target) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.