You are given a sorted integer array arr containing 1 and prime numbers, where all the integers of arr are unique. You are also given an integer k.
For every i and j where 0 <= i < j < arr.length, we consider the fraction arr[i] / arr[j].
Return the kth smallest fraction considered. Return your answer as an array of integers of size 2, where answer[0] == arr[i] and answer[1] == arr[j].
Example 1:
Input: arr = [1,2,3,5], k = 3 Output: [2,5] Explanation: The fractions to be considered in sorted order are: 1/5, 1/3, 2/5, 1/2, 3/5, and 2/3. The third fraction is 2/5.
Example 2:
Input: arr = [1,7], k = 1 Output: [1,7]
Constraints:
2 <= arr.length <= 10001 <= arr[i] <= 3 * 104arr[0] == 1arr[i] is a prime number for i > 0.arr are unique and sorted in strictly increasing order.1 <= k <= arr.length * (arr.length - 1) / 2O(n2) 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:
The problem asks us to find the K-th smallest fraction formed by picking two numbers from a given list. The brute force approach is to generate all possible fractions, sort them, and then pick the one at the K-th position.
Here's how the algorithm would work step-by-step:
def kthSmallestPrimeFraction(list_of_numbers, k_value):
all_fractions = []
# Generate all possible fractions from the given list
for numerator_index in range(len(list_of_numbers)):
for denominator_index in range(numerator_index + 1, len(list_of_numbers)):
numerator_value = list_of_numbers[numerator_index]
denominator_value = list_of_numbers[denominator_index]
fraction_value = numerator_value / denominator_value
all_fractions.append((fraction_value, numerator_value, denominator_value))
# Sort all generated fractions to find the k-th smallest
all_fractions.sort()
# Return the k-th smallest fraction's numerator and denominator
return [all_fractions[k_value - 1][1], all_fractions[k_value - 1][2]]We're looking for the k-th smallest fraction formed by pairs of numbers from a sorted list. Instead of generating and sorting all fractions, we can use a clever trick involving binary search to efficiently find the k-th smallest without checking every single one.
Here's how the algorithm would work step-by-step:
def kth_smallest_prime_fraction(list_of_prime_numbers, target_k):
list_length = len(list_of_prime_numbers)
low_bound_of_fraction = 0.0
high_bound_of_fraction = 1.0
best_numerator = 0
best_denominator = 1
while True:
midpoint_fraction_value = (low_bound_of_fraction + high_bound_of_fraction) / 2.0
count_of_smaller_fractions = 0
current_best_numerator = 0
# Optimize finding numerators for each denominator.
right_pointer_index = 1
for left_pointer_index in range(list_length - 1):
while right_pointer_index < list_length and list_of_prime_numbers[left_pointer_index] > midpoint_fraction_value * list_of_prime_numbers[right_pointer_index]:
right_pointer_index += 1
count_of_smaller_fractions += (list_length - right_pointer_index)
if right_pointer_index < list_length and list_of_prime_numbers[left_pointer_index] * best_denominator > list_of_prime_numbers[right_pointer_index] * best_numerator:
best_numerator = list_of_prime_numbers[left_pointer_index]
best_denominator = list_of_prime_numbers[right_pointer_index]
# If the count matches k, we've found our fraction.
if count_of_smaller_fractions == target_k:
return [best_numerator, best_denominator]
# If fewer than k fractions are smaller, we need to search higher.
elif count_of_smaller_fractions < target_k:
low_bound_of_fraction = midpoint_fraction_value
# If k or more fractions are smaller, we can potentially search lower.
else:
high_bound_of_fraction = midpoint_fraction_value
| Case | How to Handle |
|---|---|
| Input array `arr` is null or empty | The solution should explicitly check for null or empty `arr` and return an appropriate error or empty result to prevent crashes. |
| Input array `arr` has only one element | Since fractions require at least two distinct elements (numerator and denominator appearing later), an array with one element cannot form any valid fractions, and this case should be handled by returning an empty result. |
| Input `k` is less than 1 | The problem implies k is 1-indexed, so k < 1 is an invalid input and should be handled with an error or specific return value. |
| Input `k` is greater than the total number of possible fractions | The solution needs to account for this by either returning the largest possible fraction or indicating that k is out of bounds, as there won't be a k-th smallest fraction. |
| All elements in `arr` are identical | If all prime numbers are the same, no valid fractions (where denominator appears later) can be formed, requiring a specific check for this scenario. |
| The input `arr` contains non-prime numbers | The problem statement specifies `arr` is a sorted list of prime numbers; if this constraint is violated, the behavior might be undefined or incorrect if the algorithm relies on primality. |
| Large `k` value and large `arr` size | An efficient solution, likely involving binary search on the fraction value or a min-heap, is necessary to handle large inputs within typical time limits. |
| Fractions result in very small or very large decimal values close to machine epsilon | Careful use of floating-point comparisons or an alternative approach that avoids direct float comparison (like fraction comparison using cross-multiplication) is essential to avoid precision errors. |