medium The kth Factor of n
Problem Statement
Given two integers, n and k, return the kth smallest factor of n. If n has fewer factors than k factors, return -1.
A factor of a number is an integer that can divide that number without leaving a remainder.
Examples
-
Example 1:
- Input:
n = 12,k = 5 - Expected Output:
6 - Justification: The factors of 12 are 1, 2, 3, 4, 6, and 12. The fifth smallest factor is 6.
- Input:
-
Example 2:
- Input:
n = 17,k = 2 - Expected Output:
17 - Justification: The factors of 17 are 1 and 17, since 17 is a prime number. The second smallest factor is 17.
- Input:
-
Example 3:
- Input:
n = 10,k = 5 - Expected Output:
-1 - Justification: The factors of 10 are 1, 2, and 5. 10 has only 3 factors, so the answer is
-1.
- Input:
Try it yourself
Try solving this question here:
✅ Solution The kth Factor of n
Problem Statement
Given two integers, n and k, return the kth smallest factor of n. If n has fewer factors than k factors, return -1.
A factor of a number is an integer that can divide that number without leaving a remainder.
Examples
-
Example 1:
- Input:
n = 12,k = 5 - Expected Output:
6 - Justification: The factors of 12 are 1, 2, 3, 4, 6, and 12. The fifth smallest factor is 6.
- Input:
-
Example 2:
- Input:
n = 17,k = 2 - Expected Output:
17 - Justification: The factors of 17 are 1 and 17, since 17 is a prime number. The second smallest factor is 17.
- Input:
-
Example 3:
- Input:
n = 10,k = 5 - Expected Output:
-1 - Justification: The factors of 10 are 1, 2, and 5. 10 has only 3 factors, so the answer is
-1.
- Input:
Solution
To solve this problem, we will first calculate all the factors of n by iterating through numbers from 1 to n and checking if n is divisible by each of these numbers without a remainder. If a number is a factor, we increase the count of factors found so far. If the current factor is the kth smallest factor, we consider it as the answer.
This method is effective because it guarantees that we examine all potential divisors of n, thereby not missing any factors. Although this method may not be the most efficient for very large numbers due to its linear complexity, it is straightforward and reliable for a wide range of inputs.
Step-by-Step Algorithm
-
Initialize a counter: Start with a variable
factorCountset to 0. This counter will keep track of how many factors ofnwe have found. -
Iterate through potential factors: Loop through numbers
istarting from 1 up toninclusive. Eachirepresents a potential factor ofn. -
Check for divisibility: For each
i, check ifn % i == 0. This checks whetheriis a factor ofnby seeing if the division leaves no remainder. -
Increment the factor counter: If
iis a factor ofn(i.e., the condition in step 3 is true), incrementfactorCountby 1. -
Check if the current factor is the kth factor: If
factorCountequalskat any point, returni. This means we've found thekthfactor ofn. -
Finish the loop without finding the kth factor: If the loop completes and
factorCountis less thank, return -1. This indicates thatndoes not have akthfactor.
Algorithm Walkthrough
Let's consider the Input: n = 12, k = 5
-
Step 1: Initialize
factorCountto 0. -
Step 2: Start iterating from
i = 1toi = 12(inclusive).- When
i = 1,12 % 1 == 0. Factor found. IncrementfactorCountto 1. - When
i = 2,12 % 2 == 0. Factor found. IncrementfactorCountto 2. - When
i = 3,12 % 3 == 0. Factor found. IncrementfactorCountto 3. - When
i = 4,12 % 4 == 0. Factor found. IncrementfactorCountto 4. - When
i = 5,12 % 5 != 0. No action is taken since 5 is not a factor. - When
i = 6,12 % 6 == 0. Factor found. IncrementfactorCountto 5.
- When
-
Step 5: At this point, when
i = 6,factorCountequalsk(which is 5). This means we've found the 5th factor of 12. Returni, which is 6. -
The algorithm returns 6 as the output, indicating that the 5th smallest factor of 12 is 6.
Code
public class Solution {
// Method to find the kth factor of n using O(1) space
public int kthFactor(int n, int k) {
int factorCount = 0;
// Iterate through numbers from 1 to n
for (int i = 1; i <= n; ++i) {
// Check if i is a factor of n
if (n % i == 0) {
factorCount++; // Increment factor count
// If factorCount equals k, return i as the kth factor
if (factorCount == k) {
return i;
}
}
}
// If we finish the loop without finding the kth factor, return -1
return -1;
}
public static void main(String[] args) {
Solution solution = new Solution();
// Example 1
System.out.println(solution.kthFactor(12, 5)); // Output: 6
// Example 2
System.out.println(solution.kthFactor(17, 2)); // Output: 17
// Example 3
System.out.println(solution.kthFactor(10, 5)); // Output: -1
}
}
Complexity Analysis
Time Complexity:
The primary operation in this algorithm involves iterating through all numbers from 1 to n to find all factors of n. This iteration constitutes the bulk of the computational work, leading to a time complexity of On is the input number.
Space Complexity:
The algorithm uses the constant space. So, the space complexity is
🤖 Don't fully get this? Learn it with Claude
Stuck on The kth Factor of n? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **The kth Factor of n** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **The kth Factor of n** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **The kth Factor of n**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **The kth Factor of n**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.