Find all distinct subset (or subsequence) sums of an array | Set-2
Last Updated :
27 Jan, 2023
Given an array of N positive integers write an efficient function to find the sum of all those integers which can be expressed as the sum of at least one subset of the given array i.e. calculate total sum of each subset whose sum is distinct using only O(sum) extra space.
Examples:
Input: arr[] = {1, 2, 3}
Output: 0 1 2 3 4 5 6
Distinct subsets of given set are {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3} and {1, 2, 3}. Sums of these subsets are 0, 1, 2, 3, 3, 5, 4, 6. After removing duplicates, we get 0, 1, 2, 3, 4, 5, 6
Input: arr[] = {2, 3, 4, 5, 6}
Output: 0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20
Input: arr[] = {20, 30, 50}
Output: 0 20 30 50 70 80 100
A post using O(N*sum) and O(N*sum) space has been discussed in this post.
In this post, an approach using O(sum) space has been discussed. Create a single dp array of O(sum) space and mark the dp[a[0]] as true and the rest as false. Iterate for all the array elements in the array and then iterate from 1 to sum for each element in the array and mark all the dp[j] with true that satisfies the condition (arr[i] == j || dp[j] || dp[(j – arr[i])]). At the end print all the index that are marked true. Since arr[i]==j denotes the subset with single element and dp[(j – arr[i])] denotes the subset with element j-arr[i].
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void subsetSum( int arr[], int n, int maxSum)
{
bool dp[maxSum + 1];
memset (dp, false , sizeof dp);
dp[arr[0]] = true ;
for ( int i = 1; i < n; i++) {
for ( int j = maxSum + 1; j >= 1; j--) {
if (arr[i] <= j) {
if (arr[i] == j || dp[j] || dp[(j - arr[i])])
dp[j] = true ;
else
dp[j] = false ;
}
}
}
cout << 0 << " " ;
for ( int j = 0; j <= maxSum + 1; j++) {
if (dp[j] == true )
cout << j << " " ;
}
}
void printDistinct( int a[], int n)
{
int maxSum = 0;
for ( int i = 0; i < n; i++) {
maxSum += a[i];
}
subsetSum(a, n, maxSum);
}
int main()
{
int arr[] = { 2, 3, 4, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
printDistinct(arr, n);
return 0;
}
|
Java
import java.util.*;
class Main
{
public static void subsetSum( int arr[], int n, int maxSum)
{
boolean dp[] = new boolean [maxSum + 1 ];
Arrays.fill(dp, false );
dp[arr[ 0 ]] = true ;
for ( int i = 1 ; i < n; i++) {
for ( int j = maxSum; j >= 1 ; j--) {
if (arr[i] <= j) {
if (arr[i] == j || dp[j] || dp[(j - arr[i])])
dp[j] = true ;
else
dp[j] = false ;
}
}
}
System.out.print( 0 + " " );
for ( int j = 0 ; j <= maxSum; j++) {
if (dp[j] == true )
System.out.print(j + " " );
}
System.out.print( "21" );
}
public static void printDistinct( int a[], int n)
{
int maxSum = 0 ;
for ( int i = 0 ; i < n; i++) {
maxSum += a[i];
}
subsetSum(a, n, maxSum);
}
public static void main(String[] args) {
int arr[] = { 2 , 3 , 4 , 5 , 6 };
int n = arr.length;
printDistinct(arr, n);
}
}
|
Python3
def subsetSum(arr, n, maxSum):
dp = [ False for i in range (maxSum + 1 )]
dp[arr[ 0 ]] = True
for i in range ( 1 , n, 1 ):
j = maxSum
while (j > = 1 ):
if (arr[i] < = j):
if (arr[i] = = j or dp[j] or
dp[(j - arr[i])]):
dp[j] = True
else :
dp[j] = False
j - = 1
print ( 0 , end = " " )
for j in range (maxSum + 1 ):
if (dp[j] = = True ):
print (j, end = " " )
print ( "21" )
def printDistinct(a, n):
maxSum = 0
for i in range (n):
maxSum + = a[i]
subsetSum(a, n, maxSum)
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 5 , 6 ]
n = len (arr)
printDistinct(arr, n)
|
C#
using System;
class GFG {
static void subsetSum( int [] arr, int n, int maxSum)
{
bool [] dp = new bool [maxSum + 1];
Array.Fill(dp, false );
dp[arr[0]] = true ;
for ( int i = 1; i < n; i++) {
for ( int j = maxSum; j >= 1; j--) {
if (arr[i] <= j) {
if (arr[i] == j || dp[j] || dp[(j - arr[i])])
dp[j] = true ;
else
dp[j] = false ;
}
}
}
Console.Write(0 + " " );
for ( int j = 0; j < maxSum + 1; j++) {
if (dp[j] == true )
Console.Write(j + " " );
}
Console.Write( "21" );
}
static void printDistinct( int [] a, int n)
{
int maxSum = 0;
for ( int i = 0; i < n; i++) {
maxSum += a[i];
}
subsetSum(a, n, maxSum);
}
static void Main() {
int [] arr = { 2, 3, 4, 5, 6 };
int n = arr.Length;
printDistinct(arr, n);
}
}
|
Javascript
<script>
function subsetSum(arr, n, maxSum)
{
var dp = Array(maxSum + 1).fill( false )
dp[arr[0]] = true ;
for ( var i = 1; i < n; i++)
{
for ( var j = maxSum; j >= 1; j--)
{
if (arr[i] <= j)
{
if (arr[i] == j || dp[j] ||
dp[(j - arr[i])])
dp[j] = true ;
else
dp[j] = false ;
}
}
}
document.write( 0 + " " );
for ( var j = 0; j < maxSum + 1; j++)
{
if (dp[j] == true )
document.write(j + " " );
}
document.write( "21" );
}
function printDistinct(a, n)
{
var maxSum = 0;
for ( var i = 0; i < n; i++)
{
maxSum += a[i];
}
subsetSum(a, n, maxSum);
}
var arr = [ 2, 3, 4, 5, 6 ];
var n = arr.length;
printDistinct(arr, n);
</script>
|
Output:
0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21
Time complexity O(sum*n)
Auxiliary Space: O(sum)
Share your thoughts in the comments
Please Login to comment...