Smallest factorial number | Practice | GeeksforGeeks

Given a number n. The task is to find the smallest number whose factorial contains at least n trailing zeroes.
Example 1:
Input:
n = 1
Output: 5
Explanation : 5! = 120 which has at
least 1 trailing 0.
Example 2:
Input:
n = 6
Output: 25
Explanation : 25! has at least
6 trailing 0.


User Task:
Complete the function findNum() which takes an integer N as input parameters, and returns the answer.
Expected Time Complexity: O(log2 N * log5 N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= n <= 104
0
amarrajsmart1972 weeks ago
//Using Two Pointer Algorithm.
  int trailingZero(int n)       {           int count=0;           while(n)           {               count+=n/5;               n/=5;           }           return count;       }       int findNum(int n)       {       //complete the function here       int low=1,high=5*n;       int mid;       while(low<=high)       {           mid=(low+high)/2;           if(trailingZero(mid)==n&&mid%5==0)           {               return mid;           }           else if(trailingZero(mid)<n)           {               low=mid+1;           }           else           {               high=mid-1;           }       }      }
+2
vermaashitiit1 month ago
The maximum number whose factorial has n trailing zeroes is 5*n and the lowest lie in range 1 to 5*n so apply binary search to find the lowest.
 int trailz(int n){       int s=0;       while(n>=5){           s+=(n/5);           n/=5;       }       return s;   }       int findNum(int n)       {       //complete the function here        int low=1,high=5*n,mid;        while(low<=high){            mid=low+(high-low)/2;            if(trailz(mid)==n&&mid%5==0) return mid;            else if(trailz(mid)<n) low=mid+1;            else high=mid-1;        }       }
0
ethicalkashish9551 month ago
static bool isValid(int mid,int n)   {       int ans=0;       while(mid>0)       {           ans+=mid/5;           mid=mid/5;           if(ans>=n)           return true;       }       return false;   }       int findNum(int n)       {      int low=2,high=n*5,mid,ans;      while(low<=high)      {          mid=low+(high-low)/2;          if(isValid(mid,n))          {              ans=mid;              high=mid-1;          }          else          low=mid+1;      }      return ans;       }
0
mayankkannojia292 months ago
Java 1.5/3.5
int findNum(int n)   {       int min = 0;int max = 40011;int ans = Integer.MAX_VALUE;       while(true){           int mid = (min + max)/2;int sum =0;int a = mid;           while(a>4){               sum = sum + (a/5);               a = a/5;           }           if(sum >= n) ans = Math.min(ans,mid);           if(mid == min) break;           if(sum>=n) max = mid;           else min = mid;       } return ans; }
0
ilihaspatel442 months ago
// at the end low and high are same so
// can return high
       bool check(int p, int n){   int temp = p, count = 0, f = 5;   while (f <= temp)   {       count += temp/f;       f = f*5;   }   return (count >= n);}             int findNum(int n)       {       if(n==1)       return 5;              int low=0;       int high=5*n;       while(low<high){            int mid=(low+high)/2;            if(check(mid,n))            high=mid;            else             low=mid+1;       }       return high;           }
0
shashikantg3 months ago
Something Different.
 
 bool check(int mid,int n){
           int cnt = 0, f = 5;
           while(f <= mid){
               cnt += mid / f;
               f *= 5;
           }
           return (cnt >= n);
       }
       
       int findNum(int n){
           int s = 0, e = 5*n;
           int ans = -1;
           while(s <= e){
               int mid = (s+e)/2 - ((s+e)/2)%5;
               if(check(mid,n)){
                   ans = mid;
                   e = mid - 5;
               }
               else s = mid + 5;
           }
           return (ans);
        }
+2
gaurabhkumarjha271020013 months ago
int check (int mid, int n){
        
int temp_mid= mid; int flor_value= 5; intcount_trailing=0;
    
      while (flor_value <= temp_mid){
             
     count_trailing+= temp_mid/flor_value;
       flor_value*= 5;   
         }
          
      return (count_trailing >= n);
    }
        int findNum(int n)
        {
           if (n == 1)
             return 5;
            
             int low=0; int high= 5*n;
              
              while (low < high){
                  
                  int mid= low+ (high - low)/2;
                  
                  if (check (mid, n))
                     high= mid;
                     
                   else
                    low= mid+1;
              }
              
              return low;
        }
+1
aloksinghbais023 months ago
C++ solution having time complexity as O(log(N)) and space complexity as O(1) is as follows :-
 
Execution Time :- 0.2 / 1.3 sec
 
bool check(int mid,int n){           int cnt = 0, f = 5;           while(f <= mid){               cnt += mid / f;               f *= 5;           }           return (cnt >= n);       }              int findNum(int n){           if(n == 1)               return (5);                      int s = 0, e = 5*n;           int ans = -1;           while(s <= e){               int mid = (s+e)/2;                              if(check(mid,n)){                   ans = mid;                   e = mid - 1;               }               else s = mid + 1;           }           return (ans);       }
0
aloksinghbais02
This comment was deleted.
0
equbalzeeshan3 months ago
		int numZeros(int num) {
            int cnt = 0;
            while(num) {
                cnt += num / 5;
                num /= 5;
            }
            return cnt;
        }
        
        int findNum(int n) {
            int dummy = n * 5, lo = 1, hi = dummy, mid;
            while(lo < hi) {
                mid = lo + (hi - lo) / 2;
                if(numZeros(mid) < n)
                    lo = mid + 1;
                else
                    hi = mid;
            }
            return lo;
        }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.