problem in my code


  • 0
    N

    My code fails for a use case when n=1808548329
    Can some one explain why this happens?

    public int trailingZeroes(int n) {
    		if (n == 5) {
    			return 1;
    		}
    		int count = 0;
    		for (int i = 5; n / i >= 1; i = i * 5) {
    			count = count +  n / i;
    		}
    		return count;
    	}
    

  • 0
    M

    Try to add System.out.println(i + " " + n / i); in your loop for the case n = 1808548329 and you will find the problem due to overflow.


  • 0
    N

    @Molen Thank you.I get it now.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.