I just used the Sieve of Eratosthenes algorithm to pre-generate a list of all the primes.
Then, I can simply just iterate through [left, right] to find the 2 primes with the minimum difference.
code
class Solution {
public:
vector<int> closestPrimes(int left, int right) {
vector<int> primes (right+1, 1);
primes[1] = 0; // 1 is not a prime
// Sieve of Eratosthenes algorithm:
// 1. start from smallest prime number (in this case is 2)
// 2. set all its multiple's value to 0
// 3. find the next smallest prime number (e.g. 3)
// 4. repeat step 2.
// 5. repeat step 3 and 4 until the end
int i = 2;
for (int i = 2; i*i <= right; ++i){
if (primes[i] == 1) {
for (int j = i*i; j <= right; j += i) {
primes[j] = 0;
}
}
}
vector<int> ans(2, -1);
int smallest_difference = INT32_MAX;
int previous_prime = -1;
// iterate through every integer in [left, right]
// checking if it is prime
for (int i = left; i <= right; ++i) {
if (primes[i] == 1) {
// update value only if current difference
// is smaller than the smallest_difference
if (i-previous_prime < smallest_difference) {
smallest_difference = i-previous_prime;
ans[0] = previous_prime;
ans[1] = i;
}
// always set the previous_prime afterwards
previous_prime = i;
}
// we know that the difference cannot be smaller than 2
// (only exception is [2, 3])
// so we can break early if we have found it already
if (smallest_difference <= 2) {
break;
}
}
// if exists less than 2 primes in the set
// return {-1, -1}
if (ans[0] == -1) return {-1, -1};
// else return ans
return ans;
}
};
complexity
- Time:
O(right * log(log(right)))- Space:
O(right)
I did not realise this at the time, but a brute force solution gets 0 ms run-time on Leetcode,
only if you break when smallest_difference <= 2.
This is because the upper bound on the distance between twin primes is 1452 when you consider the limits in-place for this question (1 <= left <= right <= 10^6)
(which can be verified through simulation).
Therefore, this means that a brute force solution is not all that bad as you only have to iterate through a
maximum of 1452 integers and check if they are prime or not before you can guarantee a twin prime.
Code:
class Solution {
public:
bool isPrime(int n) {
if (n == 1) return false;
for (int i = 2; i < n; ++i) {
if (i*i>n){
break;
}
if (n%i == 0) {
return false;
}
}
return true;
}
vector<int> closestPrimes(int left, int right) {
vector<int> ans(2, -1);
int prev = -1;
int minDiff = INT32_MAX;
for (int i = left; i <= right; ++i) {
if (i%2==0 and i != 2){
continue;
}
if (isPrime(i)) {
if (i-prev < minDiff) {
minDiff = i-prev;
ans[0] = prev;
ans[1] = i;
}
prev = i;
}
if (minDiff <= 2){
break;
}
}
if (ans[0] == -1) return {-1, -1};
return ans;
}
};
Extra for experts:
complexity
- Time:
O(min(1452, right-left) * sqrt(right))- Space:
O(1)
learnings
whileloops are so slow (useforloops whenever you can)- the second solution is stupid