-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc2523.cpp
More file actions
33 lines (25 loc) · 817 Bytes
/
lc2523.cpp
File metadata and controls
33 lines (25 loc) · 817 Bytes
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
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> closestPrimes(int left, int right) {
int n = right;
vector<bool> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
vector<int> result;
for(int n = left; n <= right && result.size() < 2; n++) {
if (is_prime[n])
result.push_back(n);
}
if (result.size() < 2)
return {-1,-1};
return result;
}
};