Talentd
FleetCode

2523. Closest Prime Numbers in Range

Medium51.7% acceptance6 companies

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

  • left <= num1 < num2 <= right .
  • num1 and num2 are both 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 minimum num1 value or [-1, -1] if such numbers do not exist.

A number greater than 1 is called prime if it is only divisible by 1 and itself.

Example 1:

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.

Example 2:

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 <= 106

Hints open one at a time

Hint 1
Use Sieve of Eratosthenes to mark numbers that are primes.
Hint 2
Iterate from right to left and find pair with the minimum distance between marked numbers.
26:09
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def closestPrimes(self, left: int, right: int) -> List[int]:
3
Starter codePython 3Spaces: 4
Examples
Input
10
19
Input
4
6
Your code is saved on this device.Solution