Similar Problems

Similar Problems not available

Closest Prime Numbers In Range - Leetcode Solution

Companies:

LeetCode:  Closest Prime Numbers In Range Leetcode Solution

Difficulty: Medium

Topics: math  

Problem statement: Given two integers, L and R (L<=R), find the closest pair of prime numbers within the range [L,R]. If there is more than one pair of prime numbers with the same minimum difference, return them in ascending order.

Solution:

Before diving into the solution, Let's have a basic understanding of prime numbers.

Prime numbers are positive integers greater than 1 that are only divisible by 1 and themselves. The first few prime numbers are {2, 3, 5, 7, 11,...}.

Approach:

To solve this problem, we need to follow the following steps:

  1. Traverse the numbers in the given range [L,R].
  2. Check if the current number is a prime number or not.
  3. If it is a prime number, then store it in the prime number list.
  4. Find the closest pair of prime numbers in the prime number list.
  5. If there are multiple pairs of prime numbers with the same minimum difference, return them in ascending order.

Code:

Here is the Python implementation of the solution:

def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def closest_prime_numbers(L, R):
    prime_numbers = []
    for i in range(L, R+1):
        if is_prime(i):
            prime_numbers.append(i)
    min_diff = float('inf')
    min_diff_pairs = []
    for i in range(len(prime_numbers)-1):
        diff = prime_numbers[i+1] - prime_numbers[i]
        if diff < min_diff:
            min_diff = diff
            min_diff_pairs = [(prime_numbers[i], prime_numbers[i+1])]
        elif diff == min_diff:
            min_diff_pairs.append((prime_numbers[i], prime_numbers[i+1]))
    return min_diff_pairs


L = 10
R = 25
print(closest_prime_numbers(L, R))

Explanation:

The is_prime() function checks if a number is a prime number or not. It starts checking from 2 to the square root of the number. If the number is divisible by any number between 2 to the square root of the number, then it is not a prime number and the function returns False. Otherwise, it returns True.

The closest_prime_numbers() function takes L and R as input, traverses the numbers in the range [L,R], and checks if each number is a prime number using the is_prime() function. If the number is a prime number, then it appends it to the prime_numbers list.

After that, it finds the smallest difference between any two adjacent prime numbers in the prime_numbers list. For this purpose, it compares every two adjacent prime numbers and updates the minimum difference.

Finally, it returns all the pairs of prime numbers with the smallest difference.

Testing:

Let's check how the function works using an example:

Suppose, L = 10 and R = 25.

The prime numbers in the range [L,R] are {11, 13, 17, 19, 23}.

The differences between adjacent pairs of prime numbers are {2, 4, 2, 4}.

The minimum difference is 2, and there are two pairs of prime numbers with this difference: {(11, 13), (19, 23)}.

So, the function should return {(11, 13), (19, 23)}.

The above code will output the same result.

Closest Prime Numbers In Range Solution Code

1