#1492
The kth Factor of n
pupil · 570 · lc medium +29 · verified · 70.2% accepted · 1,925 likes · top 79%
Description
Given two positive integers n and k, list all divisors of n in ascending order. Return the kth divisor (1-indexed), or -1 if fewer than k divisors exist.
Example 1:
Input: n = 12, k = 3
Output: 3
Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.
Example 2:
Input: n = 7, k = 2
Output: 7
Explanation: Factors list is [1, 7], the 2nd factor is 7.
Example 3:
Input: n = 4, k = 4
Output: -1
Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1.
Code
1
2
3