> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# K-th Smallest Prime Fraction Python Solution

> Tested Python solution for LeetCode 786 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 786, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting), [Heap (Priority Queue)](/catalog/topics/heap-priority-queue). [View on LeetCode](https://leetcode.com/problems/k-th-smallest-prime-fraction/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 786   # by problem number
lcpy gen -s k_th_smallest_prime_fraction   # by problem name
```

## Problem

You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.

For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.

Return *the* `kth` *smallest fraction considered*. Return your answer as an array of integers of size `2`, where `answer[0] == arr[i]` and `answer[1] == arr[j]`.

### Examples

```
Input: arr = [1,2,3,5], k = 3
Output: [2,5]
Explanation: The fractions to be considered in sorted order are:
1/5, 1/3, 2/5, 1/2, 3/5, and 2/3.
The third fraction is 2/5.
```

```
Input: arr = [1,7], k = 1
Output: [1,7]
```

### Constraints

* 2 \<= arr.length \<= 1000
* 1 \<= arr\[i] \<= 3 \* 10^4
* arr\[0] == 1
* arr\[i] is a prime number for i > 0.
* All the numbers of arr are unique and sorted in strictly increasing order.
* 1 \<= k \<= arr.length \* (arr.length - 1) / 2

**Follow up:** Can you solve the problem with better than O(n^2) complexity?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/k_th_smallest_prime_fraction/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/k_th_smallest_prime_fraction/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from fractions import Fraction


class Solution:
    # Time: O(n * log(1/gap)) where gap is the smallest difference between two
    # distinct fractions (>= 1 / (3 * 10^4)^2), so ~31 counting passes.
    # Space: O(1)
    def kth_smallest_prime_fraction(self, arr: list[int], k: int) -> list[int]:
        n = len(arr)
        lo, hi = Fraction(0), Fraction(1)
        # Smallest gap between two distinct fractions a/b and c/d (values <= 3 * 10^4)
        # is >= 1 / (3 * 10^4)^2, so once the bracket is narrower the answer fraction
        # is isolated and the best fraction below `hi` is exactly the k-th smallest.
        limit = Fraction(1, 9 * 10**8)
        best = [arr[0], arr[-1]]
        while hi - lo >= limit:
            mid = (lo + hi) / 2
            count = 0
            i = 0
            num, den = 0, 1
            for j in range(1, n):
                while arr[i] * mid.denominator < arr[j] * mid.numerator:
                    i += 1
                count += i
                if i > 0 and num * arr[j] < arr[i - 1] * den:
                    num, den = arr[i - 1], arr[j]
            if count < k:
                lo = mid
            else:
                hi = mid
                best = [num, den]
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n \* log(1/gap)) where gap is the smallest difference between two | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.