The kth Factor of n

You are given two positive integers n and k. A factor of an integer n is defined as an integer i where n % i == 0.

Consider a list of all factors of n sorted in ascending order. Return the k^th factor in this list, or return -1 if n has less than k factors.

Follow up: Could you solve this problem in less than O(n) complexity?

Example 1
Inputn = 12, k = 3
Output3
Factors list is [1, 2, 3, 4, 6, 12], so the 3^rd factor is 3.
Example 2
Inputn = 7, k = 2
Output7
Factors list is [1, 7], so the 2^nd factor is 7.

Constraints

  • 1 <= k <= n <= 1000

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate