Solving Leetcode Interviews in Seconds with AI: Closest Prime Numbers in Range
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2523" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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
Explanation
- Generate Primes: Use the Sieve of Eratosthenes to efficiently generate all prime numbers within the given range
[left, right].- Find Minimum Difference: Iterate through the generated prime numbers to find the pair with the smallest difference. Keep track of the minimum difference found so far and the corresponding prime numbers.
- Handle No Solution: If no prime number pair is found, return
[-1, -1].
- Runtime Complexity: O(n log log n), where n is the value of
rightdue to the Sieve of Eratosthenes. Storage Complexity: O(n) to store the prime numbers and the sieve.
Code
def closestPrimes(left: int, right: int) -> list[int]:
"""
Finds the two closest prime numbers within the given range [left, right].
Args:
left: The left bound of the range (inclusive).
right: The right bound of the range (inclusive).
Returns:
A list containing the two closest prime numbers [num1, num2], or [-1, -1] if no such pair exists.
"""
def sieve_eratosthenes(n: int) -> list[bool]:
"""
Implements the Sieve of Eratosthenes to find all prime numbers up to n.
Args:
n: The upper bound of the range.
Returns:
A boolean list where primes[i] is True if i is prime, and False otherwise.
"""
primes = [True] * (n + 1)
primes[0] = primes[1] = False
for i in range(2, int(n**0.5) + 1):
if primes[i]:
for j in range(i*i, n+1, i):
primes[j] = False
return primes
primes = sieve_eratosthenes(right)
prime_numbers = []
for i in range(left, right + 1):
if primes[i]:
prime_numbers.append(i)
if len(prime_numbers) < 2:
return [-1, -1]
min_diff = float('inf')
result = [-1, -1]
for i in range(len(prime_numbers) - 1):
diff = prime_numbers[i+1] - prime_numbers[i]
if diff < min_diff:
min_diff = diff
result = [prime_numbers[i], prime_numbers[i+1]]
return result