Skip to content

2523. Closest Prime Numbers in Range 👍

  • Time: $O(n\log(\log n))$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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