Skip to content
BytePatterns

Kth Smallest Prime Fraction

MediumTwo Heaps & K-Way Merge#k-way-merge#min-heap~30m

Problem

You are given a sorted list that starts with 1 and continues with distinct primes. Every pair of positions i < j defines the fraction values[i] / values[j], which is always below 1. Return the k-th smallest of these fractions as the pair [numerator, denominator]. k is at least 1 and at most the number of pairs.

Examples

Input:  values = [1, 2, 3, 5], k = 3
Output: [2, 5]
Why:    in order the fractions are 1/5, 1/3, 2/5, 1/2, 3/5, 2/3
Input:  values = [1, 3, 7, 11, 13], k = 5
Output: [3, 11]
Why:    1/13, 1/11, 1/7, 3/13, then 3/11
Input:  values = [1, 7], k = 1
Output: [1, 7]
Why:    edge case, a single pair gives a single fraction

Hints

0 / 3

Stuck on the idea rather than the code? Kth Smallest in a Matrix covers it.