Unable to find the mistake in below code


  • 0
    S

    Below is my solution

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

    Above solution is failing for input n = 2147483647

    Another solution I found on discussion forum
    it is getting accepted. I am unable to get where is solution wrong.Please point it out.

    public int trailingZeroes(int n) {
          int sum = 0;
          int count = 1;
          while((n/Math.pow(5,count))>=1){
              sum = sum + n/(int)Math.pow(5,count);
              count = count+1;
          }
          return sum;
        }

  • 1
    C

    I guess this is due to integer flow out. val will be larger than the max value of integer. Depend on the platform, the loop may be endless.


Log in to reply
 

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