Find the k | C ++ program to find the K - th smallest factor ; Function to find the k 'th divisor ; initialize vectors v1 and v2 ; store all the divisors in the two vectors accordingly ; reverse the vector v2 to sort it in increasing order ; if k is greater than the size of vectors then no divisor can be possible ; else print the ( k - 1 ) th value of vector ; If K is lying in first vector ; If K is lying in second vector ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findkth ( int n , int k ) { vector < int > v1 ; vector < int > v2 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v1 . push_back ( i ) ; if ( i != sqrt ( n ) ) v2 . push_back ( n / i ) ; } } reverse ( v2 . begin ( ) , v2 . end ( ) ) ; if ( k > ( v1 . size ( ) + v2 . size ( ) ) ) cout << " Doesn ' t ▁ Exist " ; else { if ( k <= v1 . size ( ) ) cout << v1 [ k - 1 ] ; else cout << v2 [ k - v1 . size ( ) - 1 ] ; } } int main ( ) { int n = 15 , k = 2 ; findkth ( n , k ) ; return 0 ; }