Kth factor of a number

Moderate
0/80
Average time to solve is 10m
5 upvotes
Asked in companies
AmazonDeloitte

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Constraints:
1 <= T <= 50
1 <= N <= 10^5
1 <= K <= N

Time limit: 1 sec
Sample Input 1:
2
10 3
11 4
Sample Output 1:
5
-1
Explanation of sample input 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).
Sample Input 2:
2
6 2
13 2
Sample Output 2:
2
13
Explanation of sample input 2:
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.
Hint

Can you iterate through all the factors one by one to reach the ‘Kth’ factor?

Approaches (2)
Brute Force

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:

 

  1. Declare a variable ‘COUNT’ in which we store the ‘K’th factor and initialize it with 0.
  2. Run a loop for ‘i’ = 1 to ‘N’:
    • If the current number is a factor of ‘N’:
      • Increment ‘COUNT’ by 1.
    • If at any iteration, the value of ‘COUNT’ becomes equal to ‘K’, that means we have reached the ‘Kth’ factor:
      • Return ‘COUNT’.
  3. If the value of ‘COUNT’ remains less than ‘K’, it means, ‘Kth’ factor doesn’t exist.
    • Return -1.
Time Complexity

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)

Space Complexity

O(1).

 

Since we are not using any extra space for finding our answer. Hence the space complexity is O(1).

Code Solution
(100% EXP penalty)
Kth factor of a number
Full screen
Console