-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQue2523.cpp
More file actions
33 lines (29 loc) · 1.04 KB
/
Que2523.cpp
File metadata and controls
33 lines (29 loc) · 1.04 KB
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
// Closest Prime Number in Range
class Solution {
public:
bool isPrime(int n) {
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
vector<int> closestPrimes(int left, int right) {
int prev = -1, num1 = -1, num2 = -1;
int minDiff = INT_MAX;
for (int i = left; i <= right; i++) {
if (isPrime(i)) {
if (prev != -1 && (i - prev < minDiff)) {
num1 = prev;
num2 = i;
minDiff = i - prev;
}
prev = i;
}
}
if (num1 == -1) return {-1, -1};
return {num1, num2};// code with prince
}
};