The Wayback Machine - https://web.archive.org/web/20211211093156/https://www.geeksforgeeks.org/largest-subset-with-composite-sum-in-given-array/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Largest subset with composite sum in given Array

  • Last Updated : 10 Dec, 2021

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if the current
// sum is composite or not
bool checkComposite(int n)
{
  
    // 1 and 2 are not composite
    // number
    if (n == 1 || n == 2) {
        return false;
    }
  
    // If the number is divisible
    // by any digit except 2 and itself
    // then it's composite
    for (int i = 2; i < n; i++) {
  
        // If composite
        if (n % i == 0 && i != n) {
            return true;
        }
    }
  
    return false;
}
  
// Utility Function to find largest composite
// subset sum
void largestCompositeSum(int arr[], int n)
{
  
    // Vector to store the elments of
    // arr in order of first even then
    // odd numbers
    vector<int> temp;
  
    // Even numbers pushed first in
    // temp array
    for (int i = 0; i < n; i++) {
  
        // Even check
        if (arr[i] % 2 == 0) {
            temp.push_back(arr[i]);
        }
    }
  
    // Odd numbers pushed
    for (int i = 0; i < n; i++) {
        // Odd check
        if (arr[i] % 2 == 1) {
            temp.push_back(arr[i]);
        }
    }
  
    // To store current sum
    int cursum = 0;
  
    // To store maximum length composite
    // sum
    int maxlen = 0;
  
    for (int i = 0; i < n; i++) {
        cursum += temp[i];
  
        // If composite then update
        // cursum
        if (checkComposite(cursum)
            && cursum > maxlen) {
            maxlen = i + 1;
        }
    }
  
    cout << maxlen << endl;
  
    // Printing the required array
    for (int i = 0; i < maxlen; i++) {
        cout << temp[i] << " ";
    }
  
    return;
}
  
// Driver Code
int main()
{
  
    int n = 3;
    int arr[3] = { 8, 1, 4 };
  
    // Function called
    largestCompositeSum(arr, n);
  
    return 0;
}
Output:
2
8 4

Time Complexity: O(n2)
Auxiliary Space: O(n), required for temp array.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!