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 kth
factor in this list or return -1
if n
has less than k
factors.
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.
Constraints:
1 <= k <= n <= 1000
Follow up:
Could you solve this problem in less than O(n) complexity?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the kth factor of a number, we can simply check every number smaller than the original number to see if it divides evenly. We count how many factors we find, and stop when we reach the kth factor.
Here's how the algorithm would work step-by-step:
def find_kth_factor(number, kth_value):
factor_count = 0
for possible_factor in range(1, number + 1):
# Check if the possible factor divides the number evenly
if number % possible_factor == 0:
factor_count += 1
# Check if we have found the kth factor
if factor_count == kth_value:
return possible_factor
# If we didn't find the kth factor, return -1
return -1
To find the kth factor of a number, we want to avoid checking every single number. The trick is to realize that factors come in pairs, and we can stop checking halfway, handling both parts of the pair at once.
Here's how the algorithm would work step-by-step:
def find_kth_factor(number, kth_value):
factors = []
for i in range(1, int(number**0.5) + 1):
if number % i == 0:
factors.append(i)
# Return if we've found enough factors
if len(factors) == kth_value:
return factors[-1]
# Find the other half of factors
factor_length = len(factors)
for i in range(factor_length - 1, -1, -1):
other_factor = number // factors[i]
if other_factor != factors[i]:
factors.append(other_factor)
# Return if we've found enough factors
if len(factors) == kth_value:
return factors[-1]
# Check if the kth factor exists
if kth_value > len(factors):
return -1
else:
# Return the kth factor
return factors[kth_value - 1]
Case | How to Handle |
---|---|
n is zero | Return -1 as zero has no factors beyond itself. |
k is zero or negative | Return -1 as factor positions are positive integers. |
k is greater than the number of factors of n | Return -1 since the kth factor does not exist. |
n is 1 and k is 1 | Return 1, as 1 is the only factor of 1. |
n is a large number potentially causing integer overflow during factorization | Use long type for factor calculations or check for overflow during multiplication or division. |
n is a prime number | Only factors are 1 and n, so if k > 2, return -1. |
n is a perfect square and k is the middle factor (sqrt(n)) | Handle the square root case carefully to avoid double counting the factor. |
k is 1 | Return 1 since 1 is always the first factor. |