Smallest number with atleastn trailing zeroes infactorial

Pattern: Binary search on the answer

medium

The trailing zeroes of x! count the factors of 5 in 1..x, which is x/5 + x/25 + x/125 + … That count never goes down as x grows, so binary search the smallest x with at least n zeroes.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

Given n, return the smallest whole number x whose factorial x! ends in at least n zeroes. For example, n = 1 gives 5 (5! = 120), and n = 6 gives 25, because 24! ends in 4 zeroes but 25! ends in 6.

This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name findNum is this guide's own.

Think first

  • Where does each trailing zero of x! come from?
  • Which is rarer among 1..x, factors of 2 or factors of 5?
  • Why do the zero counts jump from 4 straight to 6?

Hints

    Brute force, and why it fails

    Try x = 0, 1, 2, … and count the fives each time: about 5n tries. Binary search needs about log₂(5n) tries.

    The key insight

    You cannot compute huge factorials, but you can count their zeroes in O(log x). A count that only grows with x is a monotonic check, so the smallest good x is a binary search.

    Dry run

    n = 6. The answer lies between 0 and 5 × 6 = 30, because 30! has at least 6 zeroes.

    Step 1 of 4

    The plan

    zeroes(x): count = 0; while x > 0: x = x / 5; count = count + x
    lo = 0, hi = 5 * n
    while lo < hi:
        mid = lo + (hi - lo) / 2
        if zeroes(mid) >= n: hi = mid
        else: lo = mid + 1
    return lo
    

    Complexity

    Time
    O(log² n)
    Space
    O(1)

    About log₂(5n) guesses, each counting fives in O(log n).

    Edge cases

    • n = 1: 5.
    • Counts that are skipped, such as n = 5: no x! has exactly 5 zeroes, so the answer is 25, the first with at least 5.
    • The answer is always a multiple of 5.
    I've built my logic, show the code

    Java

    class Solution {
        public int findNum(int n) {
            int lo = 0, hi = 5 * n;                 // 5n! has at least n trailing zeroes
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (trailingZeroes(mid) >= n) hi = mid;   // enough zeroes: try smaller
                else lo = mid + 1;
            }
            return lo;
        }
    
        private int trailingZeroes(int x) {        // count the factors of 5 in x!
            int count = 0;
            while (x > 0) { x /= 5; count += x; }
            return count;
        }
    }

    C++

    class Solution {
    public:
        int findNum(int n) {
            int lo = 0, hi = 5 * n;                 // 5n! has at least n trailing zeroes
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (trailingZeroes(mid) >= n) hi = mid;   // enough zeroes: try smaller
                else lo = mid + 1;
            }
            return lo;
        }
    
        int trailingZeroes(int x) {                // count the factors of 5 in x!
            int count = 0;
            while (x > 0) { x /= 5; count += x; }
            return count;
        }
    };

    Python

    class Solution:
        def findNum(self, n: int) -> int:
            lo, hi = 0, 5 * n                       # 5n! has at least n trailing zeroes
            while lo < hi:
                mid = (lo + hi) // 2
                if self.trailing_zeroes(mid) >= n:
                    hi = mid                        # enough zeroes: try smaller
                else:
                    lo = mid + 1
            return lo
    
        def trailing_zeroes(self, x: int) -> int:  # count the factors of 5 in x!
            count = 0
            while x:
                x //= 5
                count += x
            return count

    Common mistakes

    • Counting only x / 5, which misses the extra fives in 25, 125 and so on.
    • Computing the factorial itself, which overflows almost at once.
    • Searching for exactly n zeroes, which may not exist.

    Similar problems