LeetCode 2523: Closest Prime Numbers in Range Solution

Master LeetCode problem 2523 (Closest Prime Numbers in Range), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2523. Closest Prime Numbers in Range

Problem Explanation

Explanation

To solve this problem, we need to find two prime numbers within the given range such that their difference is minimized. We can start by generating a list of prime numbers within the range [left, right]. Then we can iterate through this list to find the closest pair of prime numbers with the smallest difference.

Here are the steps:

  1. Generate a list of prime numbers within the range [left, right].
  2. Iterate through the list of prime numbers to find the pair with the smallest difference.
  3. Return the pair with the smallest difference, or [-1, -1] if no such pair exists.

Time complexity analysis:

  • Generating prime numbers up to right will take O(right * log(log(right))) time.
  • Finding the closest pair will take O(right) time.
  • Therefore, the overall time complexity is O(right * log(log(right))).

Space complexity:

  • The space complexity is O(right) for storing the list of prime numbers.

Solution Code

class Solution {
    public int[] closestPrimeNumbers(int left, int right) {
        int[] ans = new int[]{-1, -1};
        List<Integer> primes = new ArrayList<>();
        
        for (int num = left; num <= right; num++) {
            if (isPrime(num)) {
                primes.add(num);
            }
        }
        
        int minDiff = Integer.MAX_VALUE;
        for (int i = 1; i < primes.size(); i++) {
            int diff = primes.get(i) - primes.get(i - 1);
            if (diff < minDiff) {
                minDiff = diff;
                ans[0] = primes.get(i - 1);
                ans[1] = primes.get(i);
            }
        }
        
        return ans;
    }
    
    private boolean isPrime(int num) {
        if (num <= 1) return false;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) return false;
        }
        return true;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2523 (Closest Prime Numbers in Range)?

This page provides optimized solutions for LeetCode problem 2523 (Closest Prime Numbers in Range) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2523 (Closest Prime Numbers in Range)?

The time complexity for LeetCode 2523 (Closest Prime Numbers in Range) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2523 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2523 in Java, C++, or Python.

Back to LeetCode Solutions