Skip to content
Problem 786 · LeetCode 786(opens in a new tab)

K-th Smallest Prime Fraction

MediumBinary search

You are given arr, a list sorted in increasing order that holds the number 1 followed by distinct primes. Every choice of positions i < j gives a fraction arr[i] / arr[j], which is always below 1.

Among all these fractions, find the k-th smallest. Return it as a pair [arr[i], arr[j]]: numerator first, then denominator.

Example 1

Input:  arr = [1, 3, 5, 7], k = 3
Output: [1, 3]
Why:    in order: 1/7, 1/5, 1/3, 3/7, 3/5, 5/7; the 3rd is 1/3
2 more examples and the constraints

Example 2

Input:  arr = [1, 11], k = 1
Output: [1, 11]
Why:    there is only one fraction

Example 3

Input:  arr = [1, 2, 3, 5, 7], k = 10
Output: [5, 7]
Why:    k is the last of 10 fractions, so return the largest

Constraints

  • 2 ≤ length of arr ≤ 1,000
  • arr[0] is 1; every later entry is a prime ≤ 30,000
  • arr is strictly increasing
  • 1 ≤ k ≤ n·(n − 1)/2, the number of fractions

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Build and sort every fractionO(n² log n)
  2. Min-heap of n − 1 rowsO(k log n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 1,000 numbers and the largest kVerdict
Build and sort every fractionO(n² log n)
Builds 500,000 fractions
Min-heap of n − 1 rowsO((n + k) log n)
Good when k is small
Binary search on the valueO(n log W)
Best; independent of k

n = length of arr, k = the rank asked for, W = 30,000², the inverse of the smallest gap between fractions.

Edge cases to test

  • Two numbers. [1, 11] with k = 1 has a single fraction, 1/11.
  • k = 1. The smallest fraction is always 1 over the largest prime.
  • k is the last. The largest fraction is the second largest number over the largest, like 5/7.
  • Large values. Products like 29,989 × 29,983 are near 9·10^8; they fit in a 32-bit int but not by much.
  • Row with nothing counted. When even arr[i]/arr[n−1] is above mid, j reaches n and the row adds 0.

Common mistakes

  • Resetting j to i + 1 for every row, which turns the sweep back into O(n²).
  • Returning mid itself instead of the largest real fraction ≤ mid.
  • Comparing fractions with floats for the best pair, when cross-multiplying is exact.

How to talk through it

  1. 1Clarify. Is arr sorted and distinct? Does it always start with 1? Return values or indices?
  2. 2Give the brute force. Build all n²/2 fractions and sort: O(n² log n) time and memory.
  3. 3Improve with a heap. Each denominator is a sorted row; merge rows with a heap, k pops.
  4. 4Search on the value. Guess a value, count fractions below it with two pointers, adjust the range.
  5. 5Test out loud. Run two numbers, k = 1 and the last k before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?