Return two prime numbers | Practice | GeeksforGeeks

Given an even number N (greater than 2), return two prime numbers whose sum will be equal to given number. There are several combinations possible. Print only the pair whose minimum value is the smallest among all the minimum values of pairs and print the minimum element first.
NOTE: A solution will always exist, read Goldbachs conjecture. 
Example 1:
Input: N = 74
Output: 3 71
Explaination: There are several possibilities 
like 37 37. But the minimum value of this pair 
is 3 which is smallest among all possible 
minimum values of all the pairs.
Example 2:
Input: 4
Output: 2 2
Explaination: This is the only possible 
prtitioning of 4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function primeDivision() which takes N as input parameter and returns the partition satisfying the condition.
Expected Time Complexity: O(N*log(logN))
Expected Auxiliary Space: O(N)
Constraints:
4 ≤ N ≤ 104  
0
iamalizaidi1106 days ago
simple code with simple logic

class Solution{
    static boolean isPrime(int n)
    {
        int f=0;
        for(int i=2;i<=n/2;i++)
        {
            if(n%i==0)
            f++;
        }
        return f==0;
    }
    static List<Integer> primeDivision(int N){
        List<Integer> l=new ArrayList<>();
        String s="#";
        for(int i=N-1;i>=2;i--)
        {
            if(isPrime(i))
            s+=i+"#";
        }
        for(int i=2;i<=N-1;i++)
        {
            if(s.contains("#"+i+"#") && s.contains("#"+(N-i)+"#"))
            {
                l.add(i);
                l.add(N-i);
                break;
            }
        }
        return l;
    }
}
0
cs21m0592 weeks ago
Use sieve of eratosthenes to find the prime numbers :- 
 
CODE(C++) :- 
 
 vector<int> prime(n+1 , 1);       prime[0] = prime[1] = 0;              for(int  i =  2 ; i <= sqrt(n) ;i++ )       {           for(int  j = 2 ; j*i < n ; j++ )           {               prime[i*j] = 0;           }       }              for(int i = 2 ; i <= n ; i++)       {           if(prime[i] == 1 && prime[n-i] == 1 )           {               return {i , n-i};           }       }   
0
gupta2411sumit2 weeks ago
 bool isPrime( int  n )   {       if(n==2)       {           return true ;       }              if(n%2==0)       {           return false ;       }              for( int i = 2 ; i<=sqrt(n) ; i++)       {           if(n%i==0)           {               return false ;           }       }              return true ;   }   vector<int> primeDivision(int N){       // code here              vector<int>ans ;              for( int i = 2 ; i<=N/2 ; i++)       {           if(isPrime(i) && isPrime(N-i))           {                ans.push_back(i) ;                ans.push_back(N-i) ;                return ans ;           }       }   }
0
reahaansheriff1 month ago
Python 2 pointer approach (0.5 sec)

class Solution:
    def primeDivision(self, N):
        # code here
        prime = []
        for i in range(1,N+1):
            if(i > 1):
                for j in range(2,int(i**0.5)+1):
                    if(i%j == 0):
                        break
                else:
                    prime.append(i)
        #print(prime)
        l=[]
        i=0
        j=len(prime)-1
        while(i<len(prime) and j < len(prime) and i <= j):
            if(prime[i]+prime[j] == N):
                l.append([prime[i],prime[j]])
                i+=1
                j-=1
            elif(prime[i]+prime[j] > N):
                j-=1
            elif(prime[i]+prime[j] < N):
                i+=1
        return l[0]
 
0
gujjulassr2 months ago
0.2/11.1
 
class Solution{   static List<Integer> primeDivision(int N){       // code here       ArrayList<Integer> a=new ArrayList<Integer>();       for(int i=2;i<=N;i++){           if(prime(i)){               int k=N-i;               if(prime(k)){                   a.add(i);                   a.add(k);                   return a;               }           }       }       return a;   }   public static boolean prime(int n){       for(int i=2;i*i<=n;i++){           if(n%i==0){               return false;           }       }       return true;   }}
0
adityashuklajsr092 months ago
simplest python code
import math
def isprime(n):
    if n <= 1:
        return False
    if n == 2:
        return True
    if n > 2 and n % 2 == 0:
        return False
 
    max_div = math.floor(math.sqrt(n))
    for i in range(3, 1 + max_div, 2):
        if n % i == 0:
            return False
    return True
        
class Solution:
    def primeDivision(self, N):
        # code here
        i=2 
        while 1:
            if isprime(i) and isprime(N-i):
                return i,N-i
            i+=1
0
adityashuklajsr09
This comment was deleted.
0
sujayghorpade2 months ago
Simple Java code:
 
static List<Integer> primeDivision(int N){
        // code here
        List<Integer> res = new ArrayList<>();
        //get all primes from 2 to N
        int[] primes = java.util.stream.IntStream.rangeClosed(2,N).filter(Solution::isPrime).toArray();
        int l = 0;
        int r = primes.length-1;
        while(l<=r){
            if(primes[l] + primes[r] == N){
                res.add(primes[l]);
                res.add(primes[r]);
                break;
            }else if(primes[l] + primes[r] < N){
                l++;
            }else {
                r--;
            }

        }

        return res;

    }
    //helper method to check if number is prime
    static boolean isPrime(int n){
        return java.util.stream.IntStream.rangeClosed(2,(int)Math.sqrt(n)).noneMatch(num -> n%num==0);
    }
-1
rko162 months ago
C++ Easy Solution
Total Time Taken:
0.0/3.4
bool isP(int n)
{
   if(n==1)
     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> primeDivision(int N){
  vector<int>v;
  
  for(int i=1;i<=N;i++)
  if(isP(i))
      v.push_back(i);
  
 int i=0,n=v.size()-1;
 while(i<v.size())
     if(v[i]+v[n]==N)
      return {v[i],v[n]};
     else if(v[i]+v[n]>N)
      n--;
     else
      i++;
 
 return {};
   }

0
tarunchawla74632 months ago
Easy to understand C++ solution
class Solution{
public:
    vector<int> primeDivision(int N){
        vector<bool> table(N+1,false);
        for(int i=2;i<sqrt(N);i++){
            if(table[i]==false){
                int t=i;
                for(int j=i*t;j<N;j=i*(++t)){
                    table[j]=true;
                }
            }
        }
        vector<int> prime;
        unordered_set<int> set;
        for(int i=2;i<N;i++){
            if(!table[i]){
                prime.push_back(i);
                set.insert(i);
            }
        }
        vector<int> re;
        for(int i=prime.size()-1;i>=0;i--){
            if(set.count(N-prime[i])){
                re.push_back(N-prime[i]);
                re.push_back(prime[i]);
                break;
            }
        }
        return re;
    }
};
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.