-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeSum.cpp
More file actions
36 lines (25 loc) · 850 Bytes
/
PrimeSum.cpp
File metadata and controls
36 lines (25 loc) · 850 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
34
35
36
/*
Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number.
NOTE A solution will always exist. read Goldbach’s conjecture
Example:
Input : 4
Output: 2 + 2 = 4
If there are more than one solutions possible, return the lexicographically smaller solution.
If [a, b] is one solution with a <= b,
and [c,d] is another solution with c <= d, then
[a, b] < [c, d]
If a < c OR a==c AND b < d.
LINK: https://www.interviewbit.com/problems/prime-sum/
*/
vector<int> Solution::primesum(int A) {
vector<bool> primes(A+1,1);
primes[0] = primes[1] = 0;
for(int i = 2; i*i<=A; i++){
if(primes[i]==1){
for(int j = i*i; j<=A; j = j+i) primes[j] = 0;
}
}
for(int i = 0; i<=A; i++){
if(primes[i]==1 && primes[A-i]==1) return {i,A-i};
}
}