Skip to content

786. K-th Smallest Prime Fraction #174

Discussion options

You must be logged in to vote

Here is a detailed breakdown:

Approach:

  1. Binary Search on Fractions:
    We perform a binary search over the range of possible fraction values, starting from 0.0 to 1.0. For each midpoint m, we count how many fractions are less than or equal to m and track the largest fraction in that range.

  2. Counting Fractions:
    Using two pointers, for each prime arr[i], we find the smallest arr[j] such that arr[i] / arr[j] is greater than the current midpoint m. We keep track of the number of valid fractions found and update the fraction closest to m but smaller than m.

  3. Binary Search Adjustments:
    If the number of fractions less than or equal to m is exactly k, we return the best fraction found so far. If…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 16, 2024
Maintainer Author

@basharul-siddike
Comment options

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants