Last updated 4 years ago
Was this helpful?
Given an integer n, return the number of trailing zeroes in n!.
n
n!
Could you write a solution that works in logarithmic time complexity?
1 <= n <= 104
Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Input: n = 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Input: n = 0
/** * Time complexity : * Space complexity : */ class Solution { public int trailingZeroes(int n) { if(n <= 0) return 0; int count = 0; for(int i = 5; n/i >= 1; i *= 5) { count += n/i; } return count; } }