You’re given a number N and a number K. Our task is to find the kth smallest divisor of N.
Input : N = 12, K = 5 Output : 6 The divisors of 12 after sorting are 1, 2, 3, 4, 6 and 12. Where the value of 5th divisor is equal to 6. Input : N = 16, K 2 Output : 2
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 :