K-Th Smallest Prime Fraction Problem
Description
LeetCode Problem 786.
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 k^th 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 <= 1000
- 1 <= arr[i] <= 3 * 10^4
- arr[0] == 1
- arr[i] is a prime number for i > 0.
- All the numbers of arr are unique and sorted in strictly increasing order.
- 1 <= k <= arr.length * (arr.length - 1) / 2
Solution
Approach: Binary Search on the Value
Listing and sorting all n(n - 1) / 2 fractions takes O(n2 log n) time, and a heap that pops k times is O(k log n), with k up to about 500,000. Instead, binary search on the value of the answer.
All fractions lie in (0, 1). For a guess mid, count how many fractions are <= mid:
- If the count is less than
k, the answer is larger thanmid: search the upper half. - If it is more than
k, search the lower half. - If it is exactly
k, the answer is the largest fraction<= mid, so track that fraction while counting.
Counting is a two-pointer sweep. For numerator index i, let j be the first denominator index with arr[i] / arr[j] <= mid. Because arr is sorted, the denominators from j to n - 1 all qualify, adding n - j to the count, and the largest of them is arr[i] / arr[j]. As i grows, the numerator grows, so j only moves right and the whole sweep is O(n). Write the test as arr[i] > mid * arr[j] to avoid a division, and compare candidate fractions by cross-multiplying, arr[i] * bestDen > bestNum * arr[j] (at most 9 · 108, which fits in 32 bits).
The search always ends with a count of exactly k: the fractions are all different, because the values are 1 and distinct primes, and two neighboring fractions differ by at least 1 / (30000 * 30000), about 10-9. Halving the interval reaches that width after about 30 steps, well within double precision.
Complexity: O(n log W) time, where n is the length of arr and W = max(arr)2 bounds how many halvings the search needs, and O(1) extra space.
class Solution:
def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:
n = len(arr)
low, high = 0.0, 1.0 # every fraction lies in (0, 1)
while True:
mid = (low + high) / 2
# Count the fractions <= mid, and find the largest of them.
count = 0
best_num, best_den = 0, 1
j = 1 # first denominator index with arr[i] / arr[j] <= mid
for i in range(n - 1):
while j < n and arr[i] > mid * arr[j]:
j += 1
count += n - j # arr[i] / arr[j], ..., arr[i] / arr[n - 1] are all <= mid
# Compare arr[i] / arr[j] with best_num / best_den without division.
if j < n and arr[i] * best_den > best_num * arr[j]:
best_num, best_den = arr[i], arr[j]
if count == k:
return [best_num, best_den] # the k-th smallest is the largest one <= mid
if count < k:
low = mid
else:
high = mid
Now write it from memory
Close the write-up and solve it yourself against the tests.