Problem: Closest Prime Numbers in Range

Given two positive integers `left` and `right`, find the two integers `num1`
and `num2` such that:

* `left <= num1 < num2 <= right `.
* `num1` and `num2` are both 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 minimum `num1`
value or `[-1, -1]` if such numbers do not exist.

A number greater than `1` is called prime if it is only divisible by `1` and
itself.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from typing import List, Dict, Tuple, Iterator

class Solution:
  def closestPrimes(self, left: int, right: int) -> List[int]:
    isPrime = self._sieveEratosthenes(right + 1)
    primes=[]
    for i in range(left, right+1):
      if isPrime[i]:
        primes.append(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


Java solution:
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 = primes.get(i) - primes.get(i - 1);
      if (diff < minDiff) {
        minDiff = diff;
        num1 = primes.get(i - 1);
        num2 = primes.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;
  }
}


C++ solution:
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;
  }
};