K-th Smallest Prime Fraction

medium heap priority-queue arrays

Problem

Given a sorted list of distinct primes (and 1) and integer K, find the K-th smallest fraction p/q where p < q are entries in the list.

Inputarr=[1,2,3,5], K=3
Output[2,5]
The sorted fractions begin 1/5, 1/3, 2/5; the third is 2/5.

import heapq
def kthSmallestPrimeFraction(arr, K):
    n = len(arr)
    h = [(arr[0]/arr[j], 0, j) for j in range(1, n)]
    heapq.heapify(h)
    for _ in range(K-1):
        _, i, j = heapq.heappop(h)
        if i + 1 < j:
            heapq.heappush(h, (arr[i+1]/arr[j], i+1, j))
    _, i, j = h[0]
    return [arr[i], arr[j]]
function kthSmallestPrimeFraction(arr, K) {
  const n = arr.length;
  const h = [];
  for (let j = 1; j < n; j++) h.push([arr[0]/arr[j], 0, j]);
  const cmp = (a,b) => a[0]-b[0];
  for (let k = 0; k < K-1; k++) {
    h.sort(cmp);
    const [_, i, j] = h.shift();
    if (i+1 < j) h.push([arr[i+1]/arr[j], i+1, j]);
  }
  h.sort(cmp);
  const [, i, j] = h[0];
  return [arr[i], arr[j]];
}
int[] kthSmallestPrimeFraction(int[] arr, int K) {
    int n = arr.length;
    PriorityQueue<double[]> pq = new PriorityQueue<>((a,b) -> Double.compare(a[0], b[0]));
    for (int j = 1; j < n; j++) pq.offer(new double[]{(double)arr[0]/arr[j], 0, j});
    for (int k = 0; k < K-1; k++) {
        double[] cur = pq.poll();
        int i = (int)cur[1], j = (int)cur[2];
        if (i+1 < j) pq.offer(new double[]{(double)arr[i+1]/arr[j], i+1, j});
    }
    double[] top = pq.peek();
    return new int[]{arr[(int)top[1]], arr[(int)top[2]]};
}
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int K) {
    int n = arr.size();
    using T = tuple<double,int,int>;
    priority_queue<T, vector<T>, greater<>> pq;
    for (int j = 1; j < n; j++) pq.push({(double)arr[0]/arr[j], 0, j});
    for (int k = 0; k < K-1; k++) {
        auto [_, i, j] = pq.top(); pq.pop();
        if (i+1 < j) pq.push({(double)arr[i+1]/arr[j], i+1, j});
    }
    auto [_, i, j] = pq.top();
    return {arr[i], arr[j]};
}
Time: O(K log n) Space: O(n)