Modify array by merging elements with addition such that it consists of only Primes.
Given an array arr[] consisting of positive integers, the task is to check whether we can modify the array by adding any of the elements of the array such that it consists of only Primes.
Examples:
Input: arr[] = {3, 5, 7, 21}
Output: YES
Explanation:
Add the following elements, 3+5+21
So the array becomes {7, 29} which consists of only primes.
Input: {2, 5, 12, 16}
Output: NO
Explanation:
There is no possible combination among elements which will make the array containing all primes
Input: {3, 5, 13, 22}
Output: YES
Explanation:
Only one combination is possible,
Add all elements – 3+5+13+22 = {43} which is only prime
Pre-requisites: Bitmasking-DP
Approach:
- We can solve this problem using Dynamic Programming with Bitmasks. We can represent our DP state as a mask which is subset of elements.
- So let our dp array be DP[mask] which represents whether upto this mask (the chosen elements) the subset formed will elements are primes only.
- The max number of bits in mask will be the number of elements in the array.
- We keep on marking the elements encoded as a sequence in mask (If i-th index element is selected, then i-th bit is set in the mask) and keep checking whether the current chosen element (current sum) is prime, if it is prime and all other elements are visited, then we return true, and we get the answer.
- Else if other elements are not visited, then as the current sum is prime, we just need to search for other elements which can individually or by summing up to form some primes, so we can safely mark our current sum as 0 again.
- If the mask is full (All elements are visited) and the current sum is not prime, we return false, because there is atleast one sum that is not prime.
- Recurrence:
DP[mask] = solve(curr + arr[i], mask | 1<<i)
where 0 <= i < n
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool dp[1 << 20];
bool isprime( int n)
{
if (n == 1)
return false ;
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0) {
return false ;
}
}
return true ;
}
int solve( int arr[], int curr,
int mask, int n)
{
if (isprime(curr))
{
if (mask == (1 << n) - 1)
{
return true ;
}
curr = 0;
}
if (mask == (1 << n) - 1)
{
if (!isprime(curr))
{
return false ;
}
}
if (dp[mask])
return dp[mask];
for ( int i = 0; i < n; i++)
{
if (!(mask & 1 << i))
{
if (solve(arr, curr + arr[i]
, mask | 1 << i, n))
{
return true ;
}
}
}
return dp[mask] = false ;
}
int main()
{
int arr[] = { 3, 6, 7, 13 };
int n = sizeof (arr) / sizeof (arr[0]);
if (solve(arr, 0, 0, n))
{
cout << "YES" ;
}
else
{
cout << "NO" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean []dp = new boolean [ 1 << 20 ];
static boolean isprime( int n)
{
if (n == 1 )
return false ;
for ( int i = 2 ; i * i <= n; i++)
{
if (n % i == 0 )
{
return false ;
}
}
return true ;
}
static boolean solve( int arr[], int curr,
int mask, int n)
{
if (isprime(curr))
{
if (mask == ( 1 << n) - 1 )
{
return true ;
}
curr = 0 ;
}
if (mask == ( 1 << n) - 1 )
{
if (!isprime(curr))
{
return false ;
}
}
if (dp[mask])
return dp[mask];
for ( int i = 0 ; i < n; i++)
{
if ((mask & ( 1 << i)) == 0 )
{
if (solve(arr, curr + arr[i],
mask | 1 << i, n))
{
return true ;
}
}
}
return dp[mask] = false ;
}
public static void main(String[] args)
{
int arr[] = { 3 , 6 , 7 , 13 };
int n = arr.length;
if (solve(arr, 0 , 0 , n))
{
System.out.print( "YES" );
}
else
{
System.out.print( "NO" );
}
}
}
|
Python3
dp = [ 0 ] * ( 1 << 20 )
def isprime(n):
if (n = = 1 ):
return False
for i in range ( 2 , n + 1 ):
if (n % i = = 0 ):
return False
return True
def solve(arr, curr, mask, n):
if (isprime(curr)):
if (mask = = ( 1 << n) - 1 ):
return True
curr = 0
if (mask = = ( 1 << n) - 1 ):
if (isprime(curr) = = False ):
return False
if (dp[mask] ! = False ):
return dp[mask]
for i in range (n):
if ((mask & 1 << i) = = False ):
if (solve(arr, curr + arr[i],
mask | 1 << i, n)):
return True
return (dp[mask] = = False )
arr = [ 3 , 6 , 7 , 13 ]
n = len (arr)
if (solve(arr, 0 , 0 , n)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG{
static bool []dp = new bool [1 << 20];
static bool isprime( int n)
{
if (n == 1)
return false ;
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
static bool solve( int []arr, int curr,
int mask, int n)
{
if (isprime(curr))
{
if (mask == (1 << n) - 1)
{
return true ;
}
curr = 0;
}
if (mask == (1 << n) - 1)
{
if (!isprime(curr))
{
return false ;
}
}
if (dp[mask])
return dp[mask];
for ( int i = 0; i < n; i++)
{
if ((mask & (1 << i)) == 0)
{
if (solve(arr, curr + arr[i],
mask | 1 << i, n))
{
return true ;
}
}
}
return dp[mask] = false ;
}
public static void Main(String[] args)
{
int []arr = { 3, 6, 7, 13 };
int n = arr.Length;
if (solve(arr, 0, 0, n))
{
Console.Write( "YES" );
}
else
{
Console.Write( "NO" );
}
}
}
|
Javascript
<script>
dp = Array(1 << 20).fill(0);
function isprime(n)
{
if (n == 1)
return false ;
for ( var i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
function solve(arr, curr, mask, n)
{
if (isprime(curr))
{
if (mask == (1 << n) - 1)
{
return true ;
}
curr = 0;
}
if (mask == (1 << n) - 1)
{
if (!isprime(curr))
{
return false ;
}
}
if (dp[mask])
return dp[mask];
for ( var i = 0; i < n; i++)
{
if (!(mask & 1 << i))
{
if (solve(arr, curr + arr[i],
mask | 1 << i, n))
{
return true ;
}
}
}
return dp[mask] = false ;
}
var arr = [ 3, 6, 7, 13 ]
var n = arr.length
if (solve(arr, 0, 0, n))
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(219).
Last Updated :
18 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...