Skip to main content
LeetCode 2523, Medium. Topics: Math, Number Theory, Primality Test, Sieve Theory, Prime Number Sieve. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:

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

Constraints

  • 1 <= left <= right <= 10<sup>6</sup>

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026