LeetCode – Factorial Trailing Zeroes (Java)

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

Note: Your solution should be in logarithmic time complexity.

Java Solution

public int trailingZeroes(int n) {
	if (n < 0)
		return -1;
 
	int count = 0;
	for (long i = 5; n / i >= 1; i *= 5) {
		count += n / i;
	}
 
	return count;
}
Category >> Algorithms >> Interview >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>