Given an array arr[] of N integers, the task is to find the maximum count of K, i.e, consecutive integers from 0 to K, that is present in the set S, where S contains all the possible subset-sum values of the array arr[].
Examples:
Input: arr[] = {1, 3}
Output: 2
Explanation: The possible subsets are {}, {1}, {3}, {1, 3} and their respective sums are {0, 1, 3, 4}. Therefore the maximum count of consecutive integers starting from 0 in the set containing all the subset sums is 2 (i.e, {0, 1}).Input: arr[] = {1, 1, 1, 4}
Output: 8
Explanation: The set containing all the subset sums of the given array is {0, 1, 2, 3, 4, 5, 6, 7}. Therefore the maximum count of consecutive integers starting from 0 is 8.
Naive Approach: The given problem can be solved using Dynamic Programming by maintaining all the possible subset sums in an array which and be done using Knapsack Technique. Thereafter, calculating the maximum count of consecutive integers.
Time Complexity: O(N*K) where K represents the sum of all elements in the array arr[].
Auxiliary Space: O(K)
Efficient Approach: The above problem can be solved using a Greedy Approach. Suppose the set containing all the subset sums of the array arr[] contains all integers in the range [0, X]. If a new number Y is introduced in the array, all integers in the range [Y, X + Y] will also be possible as the subset-sum. Using this observation, the given problem can be solved using the steps below:
- Sort the given array in non-decreasing order.
- Maintain a variable, say X as 0 which denotes that the integers in the range [0, X] are possible as the subset-sum of the given array arr[].
- In order to hold the continuity of consecutive integers, arr[i] <= X + 1 must hold true. Therefore, traverse the given array for each i in range [0, N), if the value of arr[i] <= X + 1, then update the value of X = X + arr[i]. Otherwise, break out of the loop.
- After completing the above steps, the count of integers in the range [0, X] i.e., (X + 1).
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find maximum count of // consecutive integers from 0 in set // S of all possible subset-sum int maxConsecutiveCnt(vector< int > arr)
{ // Stores the maximum possible integer
int X = 0;
// Sort the given array in
// non-decreasing order
sort(arr.begin(), arr.end());
// Iterate the given array
for ( int i = 0; i < arr.size(); i++) {
// If arr[i] <= X+1, then update
// X otherwise break the loop
if (arr[i] <= (X + 1)) {
X = X + arr[i];
}
else {
break ;
}
}
// Return Answer
return X + 1;
} // Driver Code int main()
{ vector< int > arr = { 1, 1, 1, 4 };
cout << maxConsecutiveCnt(arr);
return 0;
} |
// Java program for the above approach import java.util.Arrays;
class GFG {
// Function to find maximum count of
// consecutive integers from 0 in set
// S of all possible subset-sum
public static int maxConsecutiveCnt( int [] arr)
{
// Stores the maximum possible integer
int X = 0 ;
// Sort the given array in
// non-decreasing order
Arrays.sort(arr);
// Iterate the given array
for ( int i = 0 ; i < arr.length; i++) {
// If arr[i] <= X+1, then update
// X otherwise break the loop
if (arr[i] <= (X + 1 )) {
X = X + arr[i];
} else {
break ;
}
}
// Return Answer
return X + 1 ;
}
// Driver Code
public static void main(String args[]) {
int [] arr = { 1 , 1 , 1 , 4 };
System.out.println(maxConsecutiveCnt(arr));
}
} // This code is contributed by gfgking. |
# python program for the above approach # Function to find maximum count of # consecutive integers from 0 in set # S of all possible subset-sum def maxConsecutiveCnt(arr):
# Stores the maximum possible integer
X = 0
# Sort the given array in
# non-decreasing order
arr.sort()
# Iterate the given array
for i in range ( 0 , len (arr)):
# If arr[i] <= X+1, then update
# X otherwise break the loop
if (arr[i] < = (X + 1 )):
X = X + arr[i]
else :
break
# Return Answer
return X + 1
# Driver Code if __name__ = = "__main__" :
arr = [ 1 , 1 , 1 , 4 ]
print (maxConsecutiveCnt(arr))
# This code is contributed by rakeshsahni |
// C# program for the above approach using System;
class GFG
{ // Function to find maximum count of
// consecutive integers from 0 in set
// S of all possible subset-sum
public static int maxConsecutiveCnt( int [] arr)
{
// Stores the maximum possible integer
int X = 0;
// Sort the given array in
// non-decreasing order
Array.Sort(arr);
// Iterate the given array
for ( int i = 0; i < arr.Length; i++)
{
// If arr[i] <= X+1, then update
// X otherwise break the loop
if (arr[i] <= (X + 1))
{
X = X + arr[i];
}
else
{
break ;
}
}
// Return Answer
return X + 1;
}
// Driver Code
public static void Main()
{
int [] arr = { 1, 1, 1, 4 };
Console.Write(maxConsecutiveCnt(arr));
}
} // This code is contributed by gfgking. |
// JavaScript Program to implement
// the above approach
// Function to find maximum count of
// consecutive integers from 0 in set
// S of all possible subset-sum
function maxConsecutiveCnt(arr)
{
// Stores the maximum possible integer
let X = 0;
// Sort the given array in
// non-decreasing order
arr.sort( function (a, b) { return a - b })
// Iterate the given array
for (let i = 0; i < arr.length; i++) {
// If arr[i] <= X+1, then update
// X otherwise break the loop
if (arr[i] <= (X + 1)) {
X = X + arr[i];
}
else {
break ;
}
}
// Return Answer
return X + 1;
}
// Driver Code
let arr = [1, 1, 1, 4];
document.write(maxConsecutiveCnt(arr));
// This code is contributed by Potta Lokesh </script>
|
8
Time Complexity: O(N*log N)
Auxiliary Space: O(1)