K-th Smallest Prime Fraction - Problem
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].
Input & Output
Example 1 — Basic Case
$
Input:
arr = [1,2,3,5], k = 3
›
Output:
[2,5]
💡 Note:
All fractions: 1/2, 1/3, 1/5, 2/3, 2/5, 3/5. Sorted: 1/5, 1/3, 2/5, 1/2, 3/5, 2/3. The 3rd smallest is 2/5.
Example 2 — Different K Value
$
Input:
arr = [1,7], k = 1
›
Output:
[1,7]
💡 Note:
Only one fraction possible: 1/7, so the 1st smallest is 1/7.
Example 3 — Larger Array
$
Input:
arr = [1,2,3,5,7], k = 4
›
Output:
[1,2]
💡 Note:
Many fractions possible. After sorting, the 4th smallest fraction is 1/2.
Constraints
- 2 ≤ arr.length ≤ 1000
- 1 ≤ arr[i] ≤ 3 × 104
- arr[0] == 1
- arr[i] is a prime number for i > 0
- All the numbers of arr are unique and sorted in ascending order
- 1 ≤ k ≤ arr.length × (arr.length - 1) / 2
Visualization
Tap to expand
Understanding the Visualization
1
Input
Sorted array [1,2,3,5] and k=3
2
Generate
All possible fractions: 1/2, 1/3, 1/5, 2/3, 2/5, 3/5
3
Sort
Sorted fractions: 1/5, 1/3, 2/5, 1/2, 3/5, 2/3
4
Output
3rd smallest fraction: [2,5]
Key Takeaway
🎯 Key Insight: Use binary search on fraction values to efficiently find the k-th smallest without generating all fractions
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code