K-th Smallest Prime Fraction - Problem
K-th Smallest Prime Fraction

You're given a sorted array containing 1 and prime numbers, where all integers are unique. Your task is to find the k-th smallest fraction formed by dividing any element by a larger element in the array.

For every pair of indices i and j where 0 <= i < j < arr.length, we can form the fraction arr[i] / arr[j]. Among all such fractions, return the k-th smallest one as an array [numerator, denominator].

Example: For array [1, 2, 3, 5], possible fractions are: 1/2, 1/3, 1/5, 2/3, 2/5, 3/5. If k=3, we return [2, 5] since 2/5 is the 3rd smallest fraction.

Input & Output

example_1.py โ€” Basic Case
$ Input: arr = [1, 2, 3, 5], k = 3
โ€บ Output: [2, 5]
๐Ÿ’ก Note: All fractions in ascending order: 1/5, 1/3, 2/5, 1/2, 3/5, 2/3. The 3rd fraction is 2/5.
example_2.py โ€” First Fraction
$ Input: arr = [1, 7], k = 1
โ€บ Output: [1, 7]
๐Ÿ’ก Note: Only one fraction possible: 1/7, so it's the 1st smallest.
example_3.py โ€” Larger Array
$ Input: arr = [1, 2, 3, 5, 7], k = 5
โ€บ Output: [2, 7]
๐Ÿ’ก Note: With 5 numbers, we have 10 possible fractions. The 5th smallest is 2/7.

Constraints

  • 2 โ‰ค arr.length โ‰ค 1000
  • 1 โ‰ค arr[i] โ‰ค 3 ร— 104
  • arr[0] == 1
  • arr[i] is a prime number for i > 0
  • All numbers in arr are unique and sorted in ascending order
  • 1 โ‰ค k โ‰ค arr.length ร— (arr.length - 1) / 2

Visualization

Tap to expand
๐Ÿ” Binary Search on Fraction ValuesArray: [1, 2, 3, 5] โ†’ Fractions: 1/5, 1/3, 2/5, 1/2, 3/5, 2/31/50.21/30.332/50.41/20.53/50.62/30.67Binary Search Process (k = 3)Iteration 1: mid = 0.5Count โ‰ค 0.5: [1/5, 1/3, 2/5] = 3 fractions โœ“count == k, found answer: [2, 5]00.51Two-Pointer CountingFor each arr[i], find rightmostarr[j] where arr[i]/arr[j] โ‰ค midAdd (n - j) to countTime: O(n) per iterationโœ… Optimal SolutionTime: O(n log(max_val))Space: O(1)Handles large arrays efficientlyNo need to generate all fractions๐ŸŽฏ Key Insight: Binary search on values + efficient counting = optimal solution!
Understanding the Visualization
1
Set Search Range
Initialize binary search bounds from 0 to 1 (all possible fraction values)
2
Count Efficiently
For each mid value, use two pointers to count fractions โ‰ค mid in O(n) time
3
Track Best Candidate
While counting, remember the largest fraction that's still โ‰ค mid
4
Narrow Search
Adjust search range based on whether we found too few or too many fractions
5
Find Exact Match
When count equals k, return the tracked best candidate fraction
Key Takeaway
๐ŸŽฏ Key Insight: Instead of generating all O(nยฒ) fractions, we binary search on the fraction value and use two pointers to count fractions โ‰ค target in O(n) time, achieving optimal O(n log(max_val)) complexity.
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 20 Apple 15
38.5K Views
Medium-High Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen