Sum Of Prime | Practice | GeeksforGeeks

Given a number N,  find if N can be expressed as a + b  such that a and b are prime.
Note: If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= d, and a < c then  [a, b] is considered as our answer.
 
Example 1:
Input:
N = 8
Output:
3 5
Explanation:
3 and 5 are both prime and they
add up to 8.
Example 2:
Input:
N = 3
Output:
-1 -1
Explanation:
There are no solutions to the number 3.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function getPrimes() which takes an integer n as input and returns (a,b) as an array of size 2. 
Note: If no value of (a,b) satisfy the condition return (-1,-1) as an array of size 2.
 
Expected Time Complexity: O(N*loglog(N))
Expected Auxiliary Space: O(N)
 
Constraints:
3 <= N <= 106
0
raunakgiri211 day ago
C++ solution (0.75/3.4 s)
 
    bool isPrime(int n)   {       if(n<=1)return 0;       for(int i=2;i<=sqrt(n);i++)       if(n%i==0)return 0;       return 1;   }   vector<int> getPrimes(int N) {       // code here       vector<int> vec;       for(int i=1;i<=N/2;i++)       {           if(isPrime(i) && isPrime(N-i))           {               vec.push_back(i);               vec.push_back(N-i);               return vec;           }            }       vec.push_back(-1);       vec.push_back(-1);       return vec;   }
0
anutiger5 months ago
        vector<int> res(N + 1,1);
        for(int i  = 2; i <= N ; i ++){
            if(res[i] == 1){
                int j  = 2;
                while(i * j <= N){
                    res[i * j] = -1;
                    j++;
                }
            }
        }
        vector<int> tmp;
        for(int i  = 2 ; i<= N ; i ++){
            if(res[i] != -1)
                tmp.push_back(i);
        }
        int i = 0;
        int j = tmp.size() - 1;
        while(i < tmp.size() - 1){
            while(i < j){
                if(tmp[i] + tmp[j] == N){
                    return {tmp[i],tmp[j]};
                }
                else if(tmp[i] + tmp[j] > N) j--;
                else break;
            }
            i++;
        }
        return {-1,-1};
    }
0
Onkar Kadam10 months ago
Onkar Kadam
Sieve of Eratosthenes: https://uploads.disquscdn.c...
0
Chirag Soni1 year ago
Chirag Soni
class Solution {  public:    vector<int> getPrimes(int N) {        // code here
         vector<int> res1;                res1.push_back(-1);
                res1.push_back(-1);
        bool prime[N+1];        memset(prime,true,sizeof(prime));        for(int i=2;i*i<=N;i++)        {            if(prime[i])            {                for(int j=i*i;j<=N;j+=i)                {                    prime[j]=false;                }            }
        }        set<int> s;        for(int i=2;i<=N;i++)        {            if(prime[i]==true)            s.insert(i);        }        //for(auto i:v)        //cout<<i<<" ";="" for(auto="" i:s)="" {="" if(s.find(n-i)!="s.end())" {="" vector<int=""> res;                res.push_back(i);
                res.push_back(N-i);                return res;
            }        }        return res1;
    }};
+1
ghost1 year ago
ghost
time of execution : 0.25 secusing the SIEVE OF ERATOSTHENES
0
ghost1 year ago
ghost
https://uploads.disquscdn.c...
0
Jdragon jds1 year ago
Jdragon jds
even is always easy. for odd we just check (n-2) for prime
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.