Largest subset with sum of every pair as prime
Given an array A[], find a subset of maximum size in which sum of every pair of elements is a prime number. Print its length and the subset. Consider many queries for different arrays and maximum value of an element as 100000.
Examples :
Input : A[] = {2, 1, 2}
Output : 2
1 2
Explanation :
Here, we can only form subsets with size 1 and 2.
maximum sized subset = {1, 2}, 1 + 2 = 3, which
is prime number.
So, Answer = 2 (size), {1, 2} (subset)
Input : A[] = {2, 1, 1}
Output : 3
1 1 2
Explanation :
Maximum subset = {2, 1, 2}, since 1 + 2 = 3,
1 + 1 = 2, both are prime numbers.
Answer = 3 (size), {2, 1, 1} (subset).
Let’s make some observations and then move to problem. Sum of two numbers is even if and only both the numbers are either odd or even. An even number cannot be a prime number except 2. Now, if we take three numbers a, b and c, two of them should be either odd or even(Pigeonhole theorem). So, our solution exists only in two cases – (Let the subset be B)
- Case I : When B contains only two integers(>1) whose sum is a prime number.
- Case II : When B contains some number of ones(1s) and another number X, where X + 1 should be a prime(Only possible when X is an even number).
First count the number of ones in the array using a for loop.
- If the count of 1s is greater than 0, then traverse the whole the array and check if [A[i] + 1] is a prime number and (A[i] != 1), if found any, print the size of subarray as (count of 1s) +1 and all the ones(1s) and the found A[i]. Exit the program.
- If the above step fails (i.e, A[i] not found), print all the ones(1s). Exit the program.
- If above step fails (i.e, count of 1s = 0), Check every pair of elements in the array for their sum to be a prime. Print 2 and the pair of integers.
- Else Print -1.
Below is implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100001
bool isPrime[MAX] = { 0 };
int sieve()
{
for ( int p = 2; p * p < MAX; p++)
{
if (isPrime[p] == 0)
{
for ( int i = p * 2; i < MAX; i += p)
isPrime[i] = 1;
}
}
}
int findSubset( int a[], int n)
{
int cnt1 = 0;
for ( int i = 0; i < n; i++)
if (a[i] == 1)
cnt1++;
if (cnt1 > 0)
{
for ( int i = 0; i < n; i++)
{
if ((a[i] != 1) and (isPrime[a[i] + 1] == 0))
{
cout << cnt1 + 1 << endl;
for ( int j = 0; j < cnt1; j++)
cout << 1 << " " ;
cout << a[i] << endl;
return 0;
}
}
}
if (cnt1 >= 2)
{
cout << cnt1 << endl;
for ( int i = 0; i < cnt1; i++)
cout << 1 << " " ;
cout << endl;
return 0;
}
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if (isPrime[a[i] + a[j]] == 0)
{
cout << 2 << endl;
cout << a[i] << " " << a[j] << endl;
return 0;
}
}
}
cout << -1 << endl;
}
int main()
{
sieve();
int A[] = { 2, 1, 1 };
int n = sizeof (A) / sizeof (A[0]);
findSubset(A, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MAX = 100001 ;
static int []isPrime = new int [MAX];
static int sieve()
{
for ( int p = 2 ;
p * p < MAX; p++)
{
if (isPrime[p] == 0 )
{
for ( int i = p * 2 ;
i < MAX; i += p)
isPrime[i] = 1 ;
}
}
return - 1 ;
}
static int findSubset( int []a, int n)
{
int cnt1 = 0 ;
for ( int i = 0 ; i < n; i++)
if (a[i] == 1 )
cnt1++;
if (cnt1 > 0 )
{
for ( int i = 0 ; i < n; i++)
{
if ((a[i] != 1 ) &&
(isPrime[a[i] + 1 ] == 0 ))
{
System.out.println(cnt1 + 1 );
for ( int j = 0 ;
j < cnt1; j++)
System.out.print( 1 + " " );
System.out.println(a[i]);
return 0 ;
}
}
}
if (cnt1 >= 2 )
{
System.out.println(cnt1);
for ( int i = 0 ;
i < cnt1; i++)
System.out.print( 1 + " " );
System.out.println();
return 0 ;
}
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ;
j < n; j++)
{
if (isPrime[a[i] + a[j]] == 0 )
{
System.out.println( 2 );
System.out.println(a[i] +
" " + a[j]);
return 0 ;
}
}
}
System.out.println(- 1 );
return - 1 ;
}
public static void main(String args[])
{
sieve();
int []A = new int []{ 2 , 1 , 1 };
int n = A.length;
findSubset(A, n);
}
}
|
Python3
import math as mt
MAX = 100001
isPrime = [ 0 for i in range ( MAX )]
def sieve():
for p in range ( 2 , mt.ceil(mt.sqrt( MAX ))):
if (isPrime[p] = = 0 ) :
for i in range ( 2 * p, MAX , p):
isPrime[i] = 1
def findSubset(a, n):
cnt1 = 0
for i in range (n):
if (a[i] = = 1 ):
cnt1 + = 1
if (cnt1 > 0 ):
for i in range (n):
if ((a[i] ! = 1 ) and
(isPrime[a[i] + 1 ] = = 0 )):
print (cnt1 + 1 )
for j in range (cnt1):
print ( "1" , end = " " )
print (a[i])
return 0
if (cnt1 > = 2 ):
print (cnt1)
for i in range (cnt1):
print ( "1" , end = " " )
print ( "\n" )
return 0
for i in range (n):
for j in range (i + 1 , n):
if (isPrime[a[i] + a[j]] = = 0 ):
print ( 2 )
print (a[i], " " , a[j])
print ( - 1 )
sieve()
A = [ 2 , 1 , 1 ]
n = len (A)
findSubset(A, n)
|
C#
using System;
class GFG
{
static int MAX = 100001;
static int []isPrime = new int [MAX];
static int sieve()
{
for ( int p = 2;
p * p < MAX; p++)
{
if (isPrime[p] == 0)
{
for ( int i = p * 2;
i < MAX; i += p)
isPrime[i] = 1;
}
}
return -1;
}
static int findSubset( int []a, int n)
{
int cnt1 = 0;
for ( int i = 0; i < n; i++)
if (a[i] == 1)
cnt1++;
if (cnt1 > 0)
{
for ( int i = 0; i < n; i++)
{
if ((a[i] != 1) &&
(isPrime[a[i] + 1] == 0))
{
Console.WriteLine(cnt1 + 1);
for ( int j = 0; j < cnt1; j++)
Console.Write(1 + " " );
Console.WriteLine(a[i]);
return 0;
}
}
}
if (cnt1 >= 2)
{
Console.WriteLine(cnt1);
for ( int i = 0; i < cnt1; i++)
Console.Write(1 + " " );
Console.WriteLine();
return 0;
}
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if (isPrime[a[i] + a[j]] == 0)
{
Console.WriteLine(2);
Console.WriteLine(a[i] + " " + a[j]);
return 0;
}
}
}
Console.WriteLine(-1);
return -1;
}
static void Main()
{
sieve();
int []A = new int []{ 2, 1, 1 };
int n = A.Length;
findSubset(A, n);
}
}
|
PHP
<?php
$MAX = 100001;
$isPrime = array ();
for ( $i = 0;
$i < $MAX ; $i ++)
$isPrime [ $i ] = 0;
function sieve()
{
global $MAX , $isPrime ;
for ( $p = 2;
$p * $p < $MAX ; $p ++)
{
if ( $isPrime [ $p ] == 0)
{
for ( $i = $p * 2;
$i < $MAX ; $i += $p )
$isPrime [ $i ] = 1;
}
}
}
function findSubset( $a , $n )
{
$cnt1 = 0;
global $MAX , $isPrime ;
for ( $i = 0; $i < $n ; $i ++)
if ( $a [ $i ] == 1)
$cnt1 ++;
if ( $cnt1 > 0)
{
for ( $i = 0; $i < $n ; $i ++)
{
if (( $a [ $i ] != 1) and
( $isPrime [ $a [ $i ] + 1] == 0))
{
echo (( $cnt1 + 1) . "\n" );
for ( $j = 0;
$j < $cnt1 ; $j ++)
{
echo ( "1 " );
}
echo ( $a [ $i ] . "\n" );
return 0;
}
}
}
if ( $cnt1 >= 2)
{
echo (cnt1 . "\n" );
for ( $i = 0;
$i < $cnt1 ; $i ++)
echo ( "1 " );
echo ( "\n" );
return 0;
}
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i + 1;
$j < $n ; $j ++)
{
if ( $isPrime [ $a [ $i ] +
$a [ $j ]] == 0)
{
echo (2 . "\n" );
echo ( $a [ $i ] . " " .
$a [ $j ] . "\n" );
return 0;
}
}
}
echo (-1 . "\n" );
}
sieve();
$A = array (2, 1, 1);
$n = count ( $A );
findSubset( $A , $n );
?>
|
Javascript
<script>
let MAX = 100001;
let isPrime = new Array(MAX);
for (let i=0;i<MAX;i++)
{
isPrime[i]=0;
}
function sieve()
{
for (let p = 2;
p * p < MAX; p++)
{
if (isPrime[p] == 0)
{
for (let i = p * 2;
i < MAX; i += p)
isPrime[i] = 1;
}
}
return -1;
}
function findSubset(a,n)
{
let cnt1 = 0;
for (let i = 0; i < n; i++)
if (a[i] == 1)
cnt1++;
if (cnt1 > 0)
{
for (let i = 0; i < n; i++)
{
if ((a[i] != 1) &&
(isPrime[a[i] + 1] == 0))
{
document.write((cnt1 + 1)+ "<br>" );
for (let j = 0;
j < cnt1; j++)
document.write(1 + " " );
document.write(a[i]+ "<br>" );
return 0;
}
}
}
if (cnt1 >= 2)
{
document.write(cnt1);
for (let i = 0;
i < cnt1; i++)
document.write(1 + " " );
document.write( "<br>" );
return 0;
}
for (let i = 0; i < n; i++)
{
for (let j = i + 1;
j < n; j++)
{
if (isPrime[a[i] + a[j]] == 0)
{
document.write(2+ "<br>" );
document.write(a[i] +
" " + a[j]+ "<br>" );
return 0;
}
}
}
document.write(-1);
return -1;
}
sieve();
let A=[2, 1, 1 ];
let n = A.length;
findSubset(A, n);
</script>
|
Output:
3
1 1 2
Time Complexity : O(n2)
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...