The kth Factor of n (LeetCode 1492): Solution and Explanation
Given positive integers n and k, list the factors of n in ascending order and return the kth one, or -1 if n has fewer than k factors.
Intuition
The direct approach tests every i from 1 to n and counts the divisors. That is O(n) and passes for n up to 1000, but there is a neater view of the problem.
Divisors come in pairs. If d divides n, then n / d does too, and of the two numbers one is at most sqrt(n) and the other is at least sqrt(n). So every factor can be found by testing only d from 1 to sqrt(n).
Order falls out for free. As d climbs from 1 to sqrt(n), the small halves appear in ascending order. As d then walks back down, the partners n / d get larger, so the large halves also appear in ascending order. Count k down along that U-shaped walk and stop when it reaches zero.
Approach: Factor pairs, up then down
- Walk
dfrom 1 whiled * d <= n. Each timeddividesn, decrementk; ifkhits 0, returnd. - After the loop, step
dback tofloor(sqrt(n)). - If
d * d == n, decrementdonce more: the square root pairs with itself and was already counted. - Walk
ddown to 1. Each timeddividesn, decrementk; ifkhits 0, returnn / d. - If the walk ends without
kreaching 0, return -1.
Step-by-step example
n = 12, k = 3
- sqrt(12) is about 3.46, so the candidates on the way up are 1, 2 and 3.
- d = 1 divides 12 (partner 12). It is factor #1, so k = 2.
- d = 2 divides 12 (partner 6). It is factor #2, so k = 1.
- d = 3 divides 12 (partner 4). It is factor #3, so k = 0 and the answer is 3.
- The way down was never needed. Had k been 5, the walk down would visit partners 4 and then 6, and return 6.
Code: Python, Java, C++
1class Solution:
2 def kthFactor(self, n: int, k: int) -> int:
3 d = 1
4 while d * d <= n:
5 if n % d == 0:
6 k -= 1
7 if k == 0:
8 return d
9 d += 1
10 d -= 1
11 if d * d == n:
12 d -= 1
13 while d >= 1:
14 if n % d == 0:
15 k -= 1
16 if k == 0:
17 return n // d
18 d -= 1
19 return -11class Solution {
2 public int kthFactor(int n, int k) {
3 int d = 1;
4 while (d * d <= n) {
5 if (n % d == 0) {
6 k--;
7 if (k == 0) return d;
8 }
9 d++;
10 }
11 d--;
12 if (d * d == n) d--;
13 while (d >= 1) {
14 if (n % d == 0) {
15 k--;
16 if (k == 0) return n / d;
17 }
18 d--;
19 }
20 return -1;
21 }
22}1class Solution {
2public:
3 int kthFactor(int n, int k) {
4 int d = 1;
5 while (d * d <= n) {
6 if (n % d == 0) {
7 k--;
8 if (k == 0) return d;
9 }
10 d++;
11 }
12 d--;
13 if (d * d == n) d--;
14 while (d >= 1) {
15 if (n % d == 0) {
16 k--;
17 if (k == 0) return n / d;
18 }
19 d--;
20 }
21 return -1;
22 }
23};Time and space complexity
O(sqrt(n))
Each of the two walks visits at most sqrt(n) candidates.
O(1)
Only the counter d and the remaining k are stored.
Other approaches
Test every number (time O(n), space O(1))
Loop i from 1 to n, decrement k whenever i divides n, and return i when k reaches 0. It is the simplest correct answer and fine for n up to 1000.
Collect both halves into a list (time O(sqrt(n)), space O(sqrt(n)))
Gather small factors into one list and their partners into another, then join the first list with the reversed second list and index k - 1. Same O(sqrt n) time but O(sqrt n) extra space.
Common mistakes
- Counting sqrt(n) twice when n is a perfect square. For n = 36, the pair 6 x 6 is a single factor.
- Walking the large halves in the wrong direction. Partners n / d grow as d shrinks, so the second loop must count d downward.
- Starting the second loop at the d where the first loop stopped. That d already failed d * d <= n, so step back by one first.
Frequently asked questions
Why does walking d downward list the large factors in ascending order?
The partner of d is n / d, and n / d gets bigger as d gets smaller. Visiting d = 3, 2, 1 for n = 12 gives partners 4, 6, 12.
Why is checking up to sqrt(n) enough?
If d and n / d were both larger than sqrt(n), their product would exceed n. So every pair has a half at or below sqrt(n), and testing those finds every factor.
Is the O(n) loop accepted?
Yes. With n at most 1000 it runs instantly. The pair walk matters as a follow-up question, or when n is large.
What is returned when k is larger than the number of factors?
Both walks finish with k still positive, and the function returns -1.