Watch 10 video solutions for Closest Prime Numbers in Range, a medium level problem involving Math, Number Theory. This walkthrough by NeetCodeIO has 9,264 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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
Problem Overview: You are given two integers left and right. The task is to find two prime numbers within this inclusive range whose difference is the smallest among all prime pairs in the range. If fewer than two primes exist, return [-1, -1].
Approach 1: Direct Prime Check with Iteration (Time: O((R-L) * sqrt(R)), Space: O(1))
Iterate through every number from left to right and check whether it is prime. A number is prime if it has no divisors from 2 to sqrt(n). Keep track of the previous prime encountered and compute the difference with the current prime. Update the best pair whenever you find a smaller gap. This approach relies purely on arithmetic checks and constant memory, making it simple to implement when the range size is small. It directly uses concepts from math and number theory.
Approach 2: Sieve of Eratosthenes Optimization (Time: O(n log log n), Space: O(n))
Generate all prime numbers up to right using the Sieve of Eratosthenes. Create a boolean array where each index represents whether the number is prime. Start with all numbers marked prime, then mark multiples of each prime starting from 2. After building the sieve, iterate from left to right and collect primes or compare them on the fly. Track the previous prime and update the smallest difference pair. This avoids repeated square root checks and is significantly faster for large ranges.
Recommended for interviews: Start with the direct prime check to demonstrate understanding of primality testing. Interviewers usually expect the optimized approach using the sieve when the range becomes large. The sieve shows familiarity with classic number theory techniques and reduces repeated work, making it the practical solution for competitive programming and system constraints.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Prime Check with Iteration | O((R-L) * sqrt(R)) | O(1) | Small ranges or when avoiding extra memory |
| Sieve of Eratosthenes | O(n log log n) | O(n) | Large ranges where repeated prime checks are expensive |