Largest subset with composite sum in given Array
Given an array arr[] consisting of n distinct positive integers. Find the length of the largest subset in the given array which sums to a composite number and print the required array(order or printing elements does not matter).
Note: In case of multiple subsets with this largest size with the composite sum, output any of them.
Examples:
Input: arr[] = {8, 1, 4}, n=3
Output: 2, [8, 4]
Explanation: The required subset can be [8, 1] => 8 + 1 = 9 (composite number). Can also consider, [8, 4] (sum = 12 composite number). Note that [8, 1, 4] cannot be considered as it’s sum is not a composite number. Sum = 8 + 1 + 4 = 13(not a composite number).
Input: arr[] = {6, 4, 2, 3}, n=4
Output: 4, [6, 2, 4, 3]
Explanation: Sum of all elements, = 6 + 4 + 2 + 3 = 15 (composite number), which is the largest subset.
Approach: The given problem can be solved easily by considering the fact that all even numbers sum to an even number (which will always be a composite number except 2) and then adding an odd number to that sum and checking whether the sum is composite or not. Follow the steps below to solve the problem:
- Create a temp[] vector, storing all the even numbers first and then the odd numbers present in the given array.
- Create a variable cursum, initialized to zero, to store the current sum of the temp array, variable maxlen = 0, to store the maximum length of composite sum.
- Iterate over the range [0, n) using the variable i and perform the following tasks:
- Add the value temp[i] to the variable currsum.
- If the value currrsum is a composite number and currsum is greater than maxlen then set the value of maxlen as i+1.
- After performing the above steps, print the value of maxlen as the answer and first maxlen elements from the array temp[] as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkComposite( int n)
{
if (n == 1 || n == 2) {
return false ;
}
for ( int i = 2; i < n; i++) {
if (n % i == 0 && i != n) {
return true ;
}
}
return false ;
}
void largestCompositeSum( int arr[], int n)
{
vector< int > temp;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
temp.push_back(arr[i]);
}
}
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
temp.push_back(arr[i]);
}
}
int cursum = 0;
int maxlen = 0;
for ( int i = 0; i < n; i++) {
cursum += temp[i];
if (checkComposite(cursum)
&& cursum > maxlen) {
maxlen = i + 1;
}
}
cout << maxlen << endl;
for ( int i = 0; i < maxlen; i++) {
cout << temp[i] << " " ;
}
return ;
}
int main()
{
int n = 3;
int arr[3] = { 8, 1, 4 };
largestCompositeSum(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean checkComposite( int n)
{
if (n == 1 || n == 2 ) {
return false ;
}
for ( int i = 2 ; i < n; i++) {
if (n % i == 0 && i != n) {
return true ;
}
}
return false ;
}
static void largestCompositeSum( int arr[], int n)
{
Vector<Integer> temp = new Vector<Integer>();
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 0 ) {
temp.add(arr[i]);
}
}
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 1 ) {
temp.add(arr[i]);
}
}
int cursum = 0 ;
int maxlen = 0 ;
for ( int i = 0 ; i < n; i++) {
cursum += temp.get(i);
if (checkComposite(cursum)
&& cursum > maxlen) {
maxlen = i + 1 ;
}
}
System.out.print(maxlen + "\n" );
for ( int i = 0 ; i < maxlen; i++) {
System.out.print(temp.get(i)+ " " );
}
return ;
}
public static void main(String[] args)
{
int n = 3 ;
int arr[] = { 8 , 1 , 4 };
largestCompositeSum(arr, n);
}
}
|
Python
def checkComposite(n):
if (n = = 1 or n = = 2 ):
return false
for i in range ( 2 , n):
if (n % i = = 0 and i ! = n):
return True
return False
def largestCompositeSum(arr, n):
temp = []
for i in range (n):
if (arr[i] % 2 = = 0 ):
temp.append(arr[i])
for i in range (n):
if (arr[i] % 2 = = 1 ):
temp.append(arr[i])
cursum = 0 ;
maxlen = 0 ;
for i in range (n):
cursum + = temp[i]
if (checkComposite(cursum)
and cursum > maxlen):
maxlen = i + 1
print (maxlen)
l = len (temp) - maxlen
for i in range (l):
temp.remove(temp[i + maxlen])
print ( * temp)
return
n = 3
arr = [ 8 , 1 , 4 ]
largestCompositeSum(arr, n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool checkComposite( int n)
{
if (n == 1 || n == 2) {
return false ;
}
for ( int i = 2; i < n; i++) {
if (n % i == 0 && i != n) {
return true ;
}
}
return false ;
}
static void largestCompositeSum( int [] arr, int n)
{
List< int > temp = new List< int >();
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
temp.Add(arr[i]);
}
}
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
temp.Add(arr[i]);
}
}
int cursum = 0;
int maxlen = 0;
for ( int i = 0; i < n; i++) {
cursum += temp[i];
if (checkComposite(cursum) && cursum > maxlen) {
maxlen = i + 1;
}
}
Console.WriteLine(maxlen);
for ( int i = 0; i < maxlen; i++) {
Console.Write(temp[i] + " " );
}
return ;
}
public static void Main()
{
int n = 3;
int [] arr = { 8, 1, 4 };
largestCompositeSum(arr, n);
}
}
|
Javascript
<script>
function checkComposite(n) {
if (n == 1 || n == 2) {
return false ;
}
for (let i = 2; i < n; i++) {
if (n % i == 0 && i != n) {
return true ;
}
}
return false ;
}
function largestCompositeSum(arr, n) {
let temp = [];
for (let i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
temp.push(arr[i]);
}
}
for (let i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
temp.push(arr[i]);
}
}
let cursum = 0;
let maxlen = 0;
for (let i = 0; i < n; i++) {
cursum += temp[i];
if (checkComposite(cursum)
&& cursum > maxlen) {
maxlen = i + 1;
}
}
document.write(maxlen + '<br>');
for (let i = 0; i < maxlen; i++) {
document.write(temp[i] + " " );
}
return ;
}
let n = 3;
let arr = [8, 1, 4];
largestCompositeSum(arr, n);
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(n), required for temp array.
Last Updated :
24 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...