Simple Approach: A simple approach is to run a loop from 1 to √N and find all factors of N and push them into a vector. Finally, sort the vector and print the K-th value from the vector.

Note: Elements in the vector will not be sorted initially as we are pushing both factors (i) and (n/i). That is why it is needed to sort the vector before printing the K-th factor.

Below is the implementation of above approach :

C++

filter_none

editclose

play_arrow

linkbrightness_4code

// C++ program to find K-th smallest factor

#include <bits/stdc++.h>

usingnamespacestd;

// function to find the k'th divisor

voidfindkth(intn, intk)

{

// initialize a vector v

vector<longlong> v;

// store all the divisors

// so the loop will needs to run till sqrt ( n )

for(inti = 1; i <= sqrt(n); i++) {

if(n % i == 0) {

v.push_back(i);

if(i != sqrt(n))

v.push_back(n / i);

}

}

// sort the vector in an increasing order

sort(v.begin(), v.end());

// if k is greater than the size of vector

// then no divisor can be possible

if(k > v.size())

cout << "Doesn't Exist";

// else print the ( k - 1 )th value of vector

else

cout << v[k - 1];

}

// Driver code

intmain()

{

intn = 15, k = 2;

findkth(n, k);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to find K-th smallest factor

importjava.util.*;

classGFG{

// function to find the k'th divisor

staticvoidfindkth(intn, intk)

{

// initialize a vector v

Vector<Integer> v = newVector<Integer>();

// store all the divisors

// so the loop will needs to run till sqrt ( n )

for(inti = 1; i <= Math.sqrt(n); i++) {

if(n % i == 0) {

v.add(i);

if(i != Math.sqrt(n))

v.add(n / i);

}

}

// sort the vector in an increasing order

Collections.sort(v);

// if k is greater than the size of vector

// then no divisor can be possible

if(k > v.size())

System.out.print("Doesn't Exist");

// else print the ( k - 1 )th value of vector

else

System.out.print(v.get(k - 1));

}

// Driver code

publicstaticvoidmain(String[] args)

{

intn = 15, k = 2;

findkth(n, k);

}

}

// This code is contributed by 29AjayKumar

chevron_right

filter_none

Python3

filter_none

editclose

play_arrow

linkbrightness_4code

# Python3 program to find K-th smallest factor

frommath importsqrt

# function to find the k'th divisor

deffindkth(n, k):

# initialize a vector v

v =[]

# store all the divisors so the loop

# will needs to run till sqrt ( n )

p =int(sqrt(n)) +1

fori inrange(1, p, 1):

if(n %i ==0):

v.append(i)

if(i !=sqrt(n)):

v.append(n /i);

# sort the vector in an increasing order

v.sort(reverse =False)

# if k is greater than the size of vector

# then no divisor can be possible

if(k > len(v)):

print("Doesn't Exist")

# else print the (k - 1)th

# value of vector

else:

print(v[k -1])

# Driver code

if__name__ =='__main__':

n =15

k =2

findkth(n, k)

# This code is contributed by

# Surendra_Gangwar

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# program to find K-th smallest factor

usingSystem;

usingSystem.Collections.Generic;

classGFG{

// function to find the k'th divisor

staticvoidfindkth(intn, intk)

{

// initialize a vector v

List<int> v = newList<int>();

// store all the divisors

// so the loop will needs to run till sqrt ( n )

for(inti = 1; i <= Math.Sqrt(n); i++) {

if(n % i == 0) {

v.Add(i);

if(i != Math.Sqrt(n))

v.Add(n / i);

}

}

// sort the vector in an increasing order

v.Sort();

// if k is greater than the size of vector

// then no divisor can be possible

if(k > v.Count)

Console.Write("Doesn't Exist");

// else print the ( k - 1 )th value of vector

else

Console.Write(v[k - 1]);

}

// Driver code

publicstaticvoidMain(String[] args)

{

intn = 15, k = 2;

findkth(n, k);

}

}

// This code is contributed by PrinciRaj1992

chevron_right

filter_none

Output:

3

Time Complexity: √N log( √N )

Efficient Approach: An efficient approach will be to store the factors in two separate vectors. That is, factors i will be stored in a separate vector and N/i will be stored in a separate vector for all i from 1 to √N.
Now, if observed carefully, it can be seen that the first vector is already sorted in increasing order and the second vector is sorted in decreasing order. So, reverse the second vector and print the K-th element from either of the vectors in which it is lying.

Below is the implementation of the above approach:

C++

filter_none

editclose

play_arrow

linkbrightness_4code

// C++ program to find the K-th smallest factor

#include <bits/stdc++.h>

usingnamespacestd;

// Function to find the k'th divisor

voidfindkth ( intn, intk)

{

// initialize vectors v1 and v2

vector <int> v1;

vector <int> v2;

// store all the divisors in the two vectors

// accordingly

for( inti = 1 ; i <= sqrt( n ); i++ )

{

if( n % i == 0 )

{

v1.push_back ( i );

if( i != sqrt( n ) )

v2.push_back ( n / i );

}

}

// reverse the vector v2 to sort it

// in increasing order

reverse(v2.begin(), v2.end());

// if k is greater than the size of vectors

// then no divisor can be possible

if( k > (v1.size() + v2.size()))

cout << "Doesn't Exist";

// else print the ( k - 1 )th value of vector

else

{

// If K is lying in first vector

if(k <= v1.size())

cout<<v1[k-1];

// If K is lying in second vector

else

cout<<v2[k-v1.size()-1];

}

}

// Driver code

intmain()

{

intn = 15, k = 2;

findkth ( n, k) ;

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to find the K-th smallest factor

importjava.util.*;

classGFG

{

// Function to find the k'th divisor

staticvoidfindkth ( intn, intk)

{

// initialize vectors v1 and v2

Vector<Integer> v1 = newVector<Integer>();

Vector <Integer> v2 = newVector<Integer>();

// store all the divisors in the two vectors

// accordingly

for( inti = 1; i <= Math.sqrt( n ); i++ )

{

if( n % i == 0)

{

v1.add ( i );

if( i != Math.sqrt ( n ) )

v2.add ( n / i );

}

}

// reverse the vector v2 to sort it

// in increasing order

Collections.reverse(v2);

// if k is greater than the size of vectors

// then no divisor can be possible

if( k > (v1.size() + v2.size()))

System.out.print("Doesn't Exist");

// else print the ( k - 1 )th value of vector

else

{

// If K is lying in first vector

if(k <= v1.size())

System.out.print(v1.get(k - 1));

// If K is lying in second vector

else

System.out.print(v2.get(k-v1.size() - 1));

}

}

// Driver code

publicstaticvoidmain(String[] args)

{

intn = 15, k = 2;

findkth ( n, k) ;

}

}

// This code is contributed by PrinciRaj1992

chevron_right

filter_none

Python3

filter_none

editclose

play_arrow

linkbrightness_4code

# Python3 program to find the K-th

# smallest factor

importmath as mt

# Function to find the k'th divisor

deffindkth (n, k):

# initialize vectors v1 and v2

v1 =list()

v2 =list()

# store all the divisors in the

# two vectors accordingly

fori inrange(1, mt.ceil(n**(.5))):

if(n %i ==0):

v1.append(i)

if(i !=mt.ceil(mt.sqrt(n))):

v2.append(n //i)

# reverse the vector v2 to sort it

# in increasing order

v2[::-1]

# if k is greater than the size of vectors

# then no divisor can be possible

if( k > (len(v1) +len(v2))):

print("Doesn't Exist", end ="")

# else print the ( k - 1 )th value of vector

else:

# If K is lying in first vector

if(k <=len(v1)):

print(v1[k -1])

# If K is lying in second vector

else:

print(v2[k -len(v1) -1])

# Driver code

n =15

k =2

findkth (n, k)

# This code is contributed by Mohit kumar

chevron_right

filter_none

Output:

3

Time Complexity: √N

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.