Kth Smallest Factor | Practice | GeeksforGeeks

GIven two positive integers N and K. You have to find the Kth smallest factor of N. A factor of N is a positive integer which divides N. Output the Kth smallest factor of N if it exists otherwise print -1.
 
Example 1:
Input : N = 4 , K = 2
Output: 2
Explanation:
All factors of 4 are 1,2 and 4. Out of
these 2 is the 2nd smallest.
Example 2:
Input : N = 4 , K = 3
Output: 4
Explanation:
All factors of 4 are 1,2 and 4. Out of
these 4 is the 3rd smallest.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function kThSmallestFactor() which takes 2 Integers N and K as input and returns the answer.
 
Expected Time Complexity: O(sqrt(N))
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 106
1 <= K <= 106
0
bndas250220004 days ago
this can't be solved with O(1) space complexity. 
0
mayank180919992 weeks ago
    int kThSmallestFactor(int N , int K) {
        // code here
        set<int>s;
        for(int i=1;i<=sqrt(N);i++){
            if(N%i==0){
                s.insert(i);
                s.insert(N/i);
            }
        }
        if(s.size()<K){
            return -1;
        }
        int count=0,ans;
        for(auto it:s){
            if(count!=K){
              count++;
              ans=it;
            }else{
                break;
            }
        }
    return ans;
    }
0
shalini21sirothiya1 month ago
//c++
int kThSmallestFactor(int N , int K) {       // code here      set<int>s;      for(int i=1;i<=sqrt(N);i++){          if(N%i==0){              s.insert(i);              s.insert(N/i);          }      }      int counter=0,temp=0;      if(s.size()<K)      return -1;      for(auto i:s){          if(counter!=K){              counter++;              temp=i;          }          else{              break;          }              }      return temp;   }
0
hritikpatani1 month ago
 int kThSmallestFactor(int N , int K) {       set<int> s;       for(int i=1;i<=sqrt(N);i++){           if(N%i == 0){               s.insert(i);               s.insert(N/i);           }       }       if(s.size() < K)           return -1;       int counter=0,temp=0;       for(auto i : s){           if(counter != K){               counter++;               temp = i;           }           else               break;       }       return temp;   }
+1
darshansol1083 months ago
class Solution {   static int kThSmallestFactor(int n , int k) {       // code here       // System.out.println(n+k);              for(int i=1;i<=n;i++){                      if(n%i == 0){               k--;               if(k==0)                   return i;                          }       }              return -1;          }};
0
lokeshsingh7695
This comment was deleted.
0
deepanshukohli634 months ago
TIME TAKEN -: 0.4/60.6
def kThSmallestFactor(self, N , K):       # code here       l=[]       c=0       for i in range(1,int(pow(N,1/2))+1):           if N%i==0:               if N/i==i:                   l.append(i)                   c=c+1               else:                   l.append(i)                   l.append(int(N/i))                   c=c+2       l.sort()       if K>c:           return -1       else:           return l[K-1]
0
sanskargargee20a14724 months ago
Time = 0.1 / 12.8
Time Complexity O(root N)
Space Complexity O( root N)
 
 
// { Driver Code Starts#include <bits/stdc++.h>using namespace std;
 
// } Driver Code Endsclass Solution { public:   int kThSmallestFactor(int N , int K) {       // code here              vector <int> ans;              ans.push_back(1);              // Square Root of N loop to find the factor upto root N       for(int i=2 ; i*i <= N ; i++){                      if(N%i == 0)               ans.push_back(i);                  }                      int m=ans.size();       int last = ans[m - 1];              // Imp check if last*last == N then don't push back another factor to avoid redundancy
       if(last*last == N){           m=m-1; // m is reduced by 1       }              // Now adding the seconf factor of N from ans vector       for(int i=m-1 ; i>=0 ; i--){                      ans.push_back(N/ans[i]);                  }                     if(K>ans.size()){           return -1;       }                     return ans[K-1];          }};
// { Driver Code Starts.int main() {   int t;   cin >> t;   while (t--) {       int N,K;              cin>>N>>K;
       Solution ob;       cout << ob.kThSmallestFactor(N,K) << endl;   }   return 0;}  // } Driver Code Ends
+1
subhranshubehera99sb4 months ago
Java code with 100% accuracy and total time taken 3.8/48.7
 
class Solution {
   static int kThSmallestFactor(int N , int K) {
       // code here
       int c=0;
       if(K==1) {
           return 1;
       } else if(N<K) {
           return -1;
       }
       for(int i=1;i<=N;i++) {
           if(N%i==0){
               c++;
           }
           if(c==K){
               return i;
           } else if(c>K) {
               return -1;
           }
       }
       
       return -1;
   }
}
+1
aloksinghbais024 months ago
C++ solution having time complexity as O(sqrt(N)) and space complexity as O(2*sqrt(N)) is as follows :-
 
Execution Time :- 0.1 / 12.8 sec
 
int kThSmallestFactor(int N , int K) {       vector<int> sf,lf;       for(int i=1; i<=sqrt(N); i++){           if(N % i == 0){               if(N/i != i) sf.push_back(i), lf.push_back(N/i);               else sf.push_back(i);           }       }       reverse(lf.begin(),lf.end());              vector<int> fac;       for(auto x: sf) fac.push_back(x);       for(auto x: lf) fac.push_back(x);              if(K <= fac.size()) return fac[K-1];       return -1;   }
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.