

You are given two positive integers ‘N’ and ‘K’. You have to find the ‘K’-th factor of ‘N’, where the ‘K’-th factor is the ‘K’-th number in a sequence of all the factors of ‘N’ arranged in ascending order.
For example:
If N = 12 and K = 5, then we have to find the 5’th factor of 12 out of the list of all factors of 12 : [1, 2, 3, 4, 6, 12], the 5’th factor in the list is 6. Hence, the output is 6.
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then ‘T’ test cases follow:
The first and only line in each test case contains two space-separated positive integers ‘N’ and ‘K’, where 'N' is the positive integer whose factor is to be found, ‘K’ is the ‘K’th number in a sequence of all the factors of ‘N’ arranged in ascending order.
Output Format:
For each test case, return the ‘K’th factor of the given positive integer ‘N’. If the ‘K’th factor does not exist, print -1.
Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of.
1 <= T <= 50
1 <= N <= 10^5
1 <= K <= N
Time limit: 1 sec
2
10 3
11 4
5
-1
In the first test case, ‘N’ is 10, so all the factors of 10 are : [1, 2, 5, 10], ‘K’ given is 3, so, we have to find the 3rd factor of 10, which is 5. Hence, the output is 5.
In the second test case, ‘N’ is 11, so all the factors of 11 are : [1, 11], ‘K’ given is 4, but only 2 factors exist. Hence, the output is -1 (because the 4th factor does not exist).
2
6 2
13 2
2
13
In the first test case, ‘N’ is 6, so all the factors of 6 are : [1, 2, 3, 6], ‘K’ given is 2, so we have to find the 2’nd factor of 6, which is 2. Hence, the output is 2.
In the second test case, ‘N’ is 13, so all the factors of 13 are : [1, 13], ‘K’ given is 2, so we have to find the 2’nd factor of 13, which is 13. Hence, the output is 13.
Can you iterate through all the factors one by one to reach the ‘Kth’ factor?
The idea is to simply check each number from 1 to ‘X’ whether it is a factor of ‘X’ or not, where ‘X’ is the positive integer whose ‘Kth’ factor is to be found.
The steps are as follows:
O(N), where ‘N’ is the positive integer whose factor is to be found.
Iterating through the loop from 1 to ‘N’ will take ‘N’ iterations. Hence, the time complexity is O(N)
O(1).
Since we are not using any extra space for finding our answer. Hence the space complexity is O(1).