DEV Community

Karleb
Karleb

Posted on

786. K-th Smallest Prime Fraction

May 10 solution

https://leetcode.com/problems/k-th-smallest-prime-fraction/description/?envType=daily-question&envId=2024-05-14

/**
 * @param {number[]} arr
 * @param {number} k
 * @return {number[]}
 */
var kthSmallestPrimeFraction = function (arr, k) {
    let fractions = [], ans = []
    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            fractions.push([arr[i] / arr[j], arr[i], arr[j]])
        }
    }
    fractions.sort((a, b) => a[0] - b[0]);

    return [fractions[k - 1][1], fractions[k - 1][2]]
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)