Largest subset with composite sum in given Array
Last Updated :
24 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.
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 elements 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;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if the current
// sum is composite or not
static boolean 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
static void largestCompositeSum(int arr[], int n)
{
// Vector to store the elements of
// arr in order of first even then
// odd numbers
Vector<Integer> temp = new Vector<Integer>();
// Even numbers pushed first in
// temp array
for (int i = 0; i < n; i++) {
// Even check
if (arr[i] % 2 == 0) {
temp.add(arr[i]);
}
}
// Odd numbers pushed
for (int i = 0; i < n; i++) {
// Odd check
if (arr[i] % 2 == 1) {
temp.add(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.get(i);
// If composite then update
// cursum
if (checkComposite(cursum)
&& cursum > maxlen) {
maxlen = i + 1;
}
}
System.out.print(maxlen +"\n");
// Printing the required array
for (int i = 0; i < maxlen; i++) {
System.out.print(temp.get(i)+ " ");
}
return;
}
// Driver Code
public static void main(String[] args)
{
int n = 3;
int arr[] = { 8, 1, 4 };
// Function called
largestCompositeSum(arr, n);
}
}
// This code is contributed by shikhasingrajput
Python
# Python program for the above approach
# Function to check if the current
# sum is composite or not
def checkComposite(n):
# 1 and 2 are not composite
# number
if (n == 1 or n == 2):
return false
# If the number is divisible
# by any digit except 2 and itself
# then it's composite
for i in range(2, n):
# If composite
if (n % i == 0 and i != n):
return True
return False
# Utility Function to find largest composite
# subset sum
def largestCompositeSum(arr, n):
# Vector to store the elements of
# arr in order of first even then
# odd numbers
temp = []
# Even numbers pushed first in
# temp array
for i in range(n):
# Even check
if (arr[i] % 2 == 0):
temp.append(arr[i])
# Odd numbers pushed
for i in range(n):
# Odd check
if (arr[i] % 2 == 1):
temp.append(arr[i])
# To store current sum
cursum = 0;
# To store maximum length composite
# sum
maxlen = 0;
for i in range(n):
cursum += temp[i]
# If composite then update
# cursum
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])
# Printing the required array
print(*temp)
return
# Driver code
n = 3
arr = [8, 1, 4]
# Function called
largestCompositeSum(arr, n);
# This code is contributed by Samim Hossain Mondal.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to check if the current
// sum is composite or not
static 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
static void largestCompositeSum(int[] arr, int n)
{
// Vector to store the elements of
// arr in order of first even then
// odd numbers
List<int> temp = new List<int>();
// Even numbers pushed first in
// temp array
for (int i = 0; i < n; i++) {
// Even check
if (arr[i] % 2 == 0) {
temp.Add(arr[i]);
}
}
// Odd numbers pushed
for (int i = 0; i < n; i++) {
// Odd check
if (arr[i] % 2 == 1) {
temp.Add(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;
}
}
Console.WriteLine(maxlen);
// Printing the required array
for (int i = 0; i < maxlen; i++) {
Console.Write(temp[i] + " ");
}
return;
}
// Driver Code
public static void Main()
{
int n = 3;
int[] arr = { 8, 1, 4 };
// Function called
largestCompositeSum(arr, n);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript code for the above approach
// Function to check if the current
// sum is composite or not
function checkComposite(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 (let 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
function largestCompositeSum(arr, n) {
// Vector to store the elements of
// arr in order of first even then
// odd numbers
let temp = [];
// Even numbers pushed first in
// temp array
for (let i = 0; i < n; i++) {
// Even check
if (arr[i] % 2 == 0) {
temp.push(arr[i]);
}
}
// Odd numbers pushed
for (let i = 0; i < n; i++) {
// Odd check
if (arr[i] % 2 == 1) {
temp.push(arr[i]);
}
}
// To store current sum
let cursum = 0;
// To store maximum length composite
// sum
let maxlen = 0;
for (let i = 0; i < n; i++) {
cursum += temp[i];
// If composite then update
// cursum
if (checkComposite(cursum)
&& cursum > maxlen) {
maxlen = i + 1;
}
}
document.write(maxlen + '<br>');
// Printing the required array
for (let i = 0; i < maxlen; i++) {
document.write(temp[i] + " ");
}
return;
}
// Driver Code
let n = 3;
let arr = [8, 1, 4];
// Function called
largestCompositeSum(arr, n);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(n2)
Auxiliary Space: O(n), required for temp array.
Similar Reads
Largest Subset with sum less than each Array element Given an array arr[] containing N elements, the task is to find the size of the largest subset for each array element arr[i] such that the sum of the subset is less than that element. Examples: Input: arr[] = { 5, 2, 1, 1, 1, 6, 8}Output: 3 1 0 0 0 4 4 Explanation: For i = 0 -> subset = {1, 1, 1}
12 min read
Sum and product of k smallest and k largest composite numbers in the array Given an integer k and an array of integers arr, the task is to find the sum and product of k smallest and k largest composite numbers in the array. Assume that there are at least k composite numbers in the array. Examples: Input: arr[] = {2, 5, 6, 8, 10, 11}, k = 2 Output: Sum of k-minimum composit
15+ min read
Longest Increasing Subarray of Composite Numbers in Array Given an array of integers arr[], the task is to find the longest increasing subarray of composite numbers in an array. Examples: Input: arr[] = [1, 4, 7, 6, 8, 10, 12]Output: [6, 8, 10, 12]Explanation: The longest increasing subarray of composite numbers in the given array is [6, 8, 10, 12], which
12 min read
Maximum sum of Sublist with composite number nodes in a Linked List Given a linked list, the task is to find the maximum sum of a sublist with composite number nodes. A composite number is any positive integer greater than 1 that is not a prime number. Examples: Input: List: 1 -> 4 -> 8 -> 7 -> 6 -> 6 -> 9Output: 21Explanation: The sublist with com
3 min read
Largest Subset with sum at most K when one Array element can be halved Given an array arr[] of size N and an integer K, the task is to find the size of the largest subset possible having a sum at most K when only one element can be halved (the halved value will be rounded to the closest greater integer). Examples: Input: arr[] = {4, 4, 5}, K = 15Output: 3Explanation: 4
5 min read
Find largest d in array such that a + b + c = d Given a set S (all distinct elements) of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S. Constraints: 1 ? number of elements in the set ? 1000 INT_MIN ? each element in the set ? INT_MAX Examples : Input : S[] = {2, 3, 5, 7, 12} Output : 12 Expla
15+ min read