Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-225

Kth Factor

Problem

Return the k-th smallest positive divisor of n, or -1 when n has fewer than k divisors.

Collect divisors up to the square root, then append their partners in descending order to obtain the sorted divisor list. Return the k-th entry or -1.

Hint

Small divisors come in ascending order, their partners in descending order.

Test cases

case 1
in: [12,3]
expected: 3
case 2
in: [7,2]
expected: 7
case 3
in: [4,4]
expected: -1
case 4
in: [1,1]
expected: 1
case 5
in: [1000,3]
expected: 4
Starter codePython
def kth_factor(n, k):
    # Your code here
    pass

5 test cases · run your code against them in the editor