> ## 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.

# Closest Prime Numbers in Range Python Solution

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

LeetCode 2523, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Number Theory](/catalog/topics/number-theory), Primality Test, Sieve Theory, Prime Number Sieve. [View on LeetCode](https://leetcode.com/problems/closest-prime-numbers-in-range/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 2523   # by problem number
lcpy gen -s closest_prime_numbers_in_range   # by problem name
```

## Problem

Given two positive integers `left` and `right`, find the two integers `num1` and `num2` such that:

* `left <= num1 < num2 <= right`.
* Both `num1` and `num2` are prime numbers.
* `num2 - num1` is the **minimum** amongst all other pairs satisfying the above conditions.

Return the positive integer array `ans = [num1, num2]`. If there are multiple pairs satisfying these conditions, return the one with the **smallest** `num1` value. If no such numbers exist, return `[-1, -1]`.

### Examples

```
Input: left = 10, right = 19
Output: [11,13]
Explanation: The prime numbers between 10 and 19 are 11, 13, 17, and 19.
The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].
Since 11 is smaller than 17, we return the first pair.
```

```
Input: left = 4, right = 6
Output: [-1,-1]
Explanation: There exists only one prime number in the given range, so the conditions cannot be satisfied.
```

### Constraints

* 1 \<= left \<= right \<= 10\<sup>6\</sup>

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(right * log(log(right)))
    # Space: O(right)
    def closest_primes(self, left: int, right: int) -> list[int]:
        if right < 2:
            return [-1, -1]
        sieve = bytearray([1]) * (right + 1)
        sieve[0] = sieve[1] = 0
        i = 2
        while i * i <= right:
            if sieve[i]:
                sieve[i * i : right + 1 : i] = bytearray(len(sieve[i * i : right + 1 : i]))
            i += 1
        best: list[int] = [-1, -1]
        prev = -1
        for num in range(max(left, 2), right + 1):
            if not sieve[num]:
                continue
            if prev != -1 and (best[0] == -1 or num - prev < best[1] - best[0]):
                best = [prev, num]
            prev = num
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(right \* log(log(right))) | O(right) |

## Tags

[NeetCode All](/catalog/neetcode).


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