Given two positive integers left
and right
, find the two integers num1
and num2
such that:
left <= num1 < num2 <= right
.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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're looking for two prime numbers within a specified range that are as close to each other as possible. The brute force method checks every single pair of numbers in that range to see if they are prime and calculates their distance.
Here's how the algorithm would work step-by-step:
def closest_prime_numbers(left, right): closest_primes = None
min_distance = float('inf')
def is_prime(number_to_check):
if number_to_check <= 1:
return False
for i in range(2, int(number_to_check**0.5) + 1):
if number_to_check % i == 0:
return False
return True
for first_number in range(left, right + 1):
# Optimization: Skip non-prime numbers to improve performance
if not is_prime(first_number):
continue
for second_number in range(first_number + 1, right + 1):
# Optimization: Skip non-prime numbers to improve performance
if not is_prime(second_number):
continue
distance = second_number - first_number
# Update closest primes if current distance is smaller
if distance < min_distance:
min_distance = distance
closest_primes = [first_number, second_number]
# Returning none when no primes are found
return closest_primes
To find the closest prime numbers within a range efficiently, we first identify all prime numbers in that range. Then, we simply find the pair of prime numbers that are closest to each other.
Here's how the algorithm would work step-by-step:
def closest_prime_numbers(left_bound, right_bound):
number_range = list(range(left_bound, right_bound + 1))
is_prime = [True] * len(number_range)
# 0 and 1 are not prime
if left_bound <= 1:
is_prime[max(0, 1 - left_bound)] = False
if left_bound <= 0:
is_prime[max(0, 0 - left_bound)] = False
for number in range(2, int(right_bound**0.5) + 1):
# Optimization: skip if already marked non-prime
if number >= left_bound and not is_prime[number - left_bound]:
continue
# Eliminating multiples of the current prime number
start = max(number * number, ((left_bound + number - 1) // number) * number)
for multiple in range(start, right_bound + 1, number):
is_prime[multiple - left_bound] = False
prime_numbers = []
for index, value in enumerate(is_prime):
if value:
prime_numbers.append(number_range[index])
if len(prime_numbers) < 2:
return None
min_difference = float('inf')
closest_primes = None
# Find the pair of primes with the minimum difference
for i in range(len(prime_numbers) - 1):
difference = prime_numbers[i+1] - prime_numbers[i]
# Keeping track of minimum difference between primes
if difference < min_difference:
min_difference = difference
closest_primes = [prime_numbers[i], prime_numbers[i+1]]
return closest_primes
Case | How to Handle |
---|---|
Invalid input range (left > right) | Return an empty list or null if the input range is invalid, indicating no valid primes. |
Range contains no prime numbers | Return an empty list or null if no prime numbers exist within the range to indicate that no closest pair can be found. |
Input range includes negative numbers | Since prime numbers are defined for positive integers, only consider the positive portion of the range or return an empty list if the entire range is negative. |
Large input range causing potential integer overflow during calculations (e.g., in sieve) | Use a data type that supports larger numbers, like long, to avoid integer overflow issues. |
Range starts or ends with a prime number | The algorithm should correctly identify and consider these boundary prime numbers when searching for the closest pair. |
Small range with only one prime number | Return null or an empty list as no pair can be formed if there's only one prime number. |
left and right are equal and prime | Return null or an empty list as no pair can be formed if left and right are equal, there is only one prime number |
left and right are large non-primes that are close to primes | The primality test must efficiently handle near-prime numbers without excessive computation, so a sieve or optimized primality test is important. |