2523. Closest Prime Numbers in Range LeetCode Solution
In this guide, you will get 2523. Closest Prime Numbers in Range LeetCode Solution with the best time and space complexity. The solution to Closest Prime Numbers in Range problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Closest Prime Numbers in Range solution in C++
- Closest Prime Numbers in Range solution in Java
- Closest Prime Numbers in Range solution in Python
- Additional Resources
Problem Statement of Closest Prime Numbers in Range
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].
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
Complexity Analysis
- Time Complexity: O(n\log(\log n))
- Space Complexity: O(n)
2523. Closest Prime Numbers in Range LeetCode Solution in C++
class Solution {
public:
vector<int> closestPrimes(int left, int right) {
const vector<bool> isPrime = sieveEratosthenes(right + 1);
vector<int> primes;
for (int i = left; i <= right; ++i)
if (isPrime[i])
primes.push_back(i);
if (primes.size() < 2)
return {-1, -1};
int minDiff = INT_MAX;
int num1 = -1;
int num2 = -1;
for (int i = 1; i < primes.size(); ++i) {
const int diff = primes[i] - primes[i - 1];
if (diff < minDiff) {
minDiff = diff;
num1 = primes[i - 1];
num2 = primes[i];
}
}
return {num1, num2};
}
private:
vector<bool> sieveEratosthenes(int n) {
vector<bool> isPrime(n, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i < n; ++i)
if (isPrime[i])
for (int j = i * i; j < n; j += i)
isPrime[j] = false;
return isPrime;
}
};
/* code provided by PROGIEZ */
2523. Closest Prime Numbers in Range LeetCode Solution in Java
class Solution {
public int[] closestPrimes(int left, int right) {
final boolean[] isPrime = sieveEratosthenes(right + 1);
List<Integer> primes = new ArrayList<>();
for (int i = left; i <= right; ++i)
if (isPrime[i])
primes.add(i);
if (primes.size() < 2)
return new int[] {-1, -1};
int minDiff = Integer.MAX_VALUE;
int num1 = -1;
int num2 = -1;
for (int i = 1; i < primes.size(); ++i) {
final int diff = prime.get(i) - prime.get(i - 1);
if (diff < minDiff) {
minDiff = diff;
num1 = prime.get(i - 1);
num2 = prime.get(i);
}
}
return new int[] {num1, num2};
}
private boolean[] sieveEratosthenes(int n) {
boolean[] isPrime = new boolean[n];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i < n; ++i)
if (isPrime[i])
for (int j = i * i; j < n; j += i)
isPrime[j] = false;
return isPrime;
}
}
// code provided by PROGIEZ
2523. Closest Prime Numbers in Range LeetCode Solution in Python
class Solution:
def closestPrimes(self, left: int, right: int) -> list[int]:
isPrime = self._sieveEratosthenes(right + 1)
primes = [i for i in range(left, right + 1) if isPrime[i]]
if len(primes) < 2:
return [-1, -1]
minDiff = math.inf
num1 = -1
num2 = -1
for a, b in zip(primes, primes[1:]):
diff = b - a
if diff < minDiff:
minDiff = diff
num1 = a
num2 = b
return [num1, num2]
def _sieveEratosthenes(self, n: int) -> list[bool]:
isPrime = [True] * n
isPrime[0] = False
isPrime[1] = False
for i in range(2, int(n**0.5) + 1):
if isPrime[i]:
for j in range(i * i, n, i):
isPrime[j] = False
return isPrime
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.