4.Factorial Trailing Zeroes

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

Solution:

Divide a number by 5 and add all the quotients.

class Solution
{
public:
    int trailingZeroes(int n)
    {

        int count = 0;
        while (n > 0)
        {
            int p = n / 5;
            count = count + p;
            n = p;
        }
        return count;
    }
};

Time Complexity: O(log n)

Last updated