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

`// C++ program to find total sum of ` `// all distinct subset sums in O(sum) space. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print all th distinct sum ` `void` `subsetSum(` `int` `arr[], ` `int` `n, ` `int` `maxSum) ` `{ ` ` ` ` ` `// Declare a boolean array of size ` ` ` `// equal to total sum of the array ` ` ` `bool` `dp[maxSum + 1]; ` ` ` `memset` `(dp, ` `false` `, ` `sizeof` `dp); ` ` ` ` ` `// Fill the first row beforehand ` ` ` `dp[arr[0]] = ` `true` `; ` ` ` ` ` `// dp[j] will be true only if sum j ` ` ` `// can be formed by any possible ` ` ` `// addition of numbers in given array ` ` ` `// upto index i, otherwise false ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` ` ` `// Iterate from maxSum to 1 ` ` ` `// and avoid lookup on any other row ` ` ` `for` `(` `int` `j = maxSum + 1; j >= 1; j--) { ` ` ` ` ` `// Do not change the dp array ` ` ` `// for j less than arr[i] ` ` ` `if` `(arr[i] <= j) { ` ` ` `if` `(arr[i] == j || dp[j] || dp[(j - arr[i])]) ` ` ` `dp[j] = ` `true` `; ` ` ` ` ` `else` ` ` `dp[j] = ` `false` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// If dp [j] is true then print ` ` ` `cout << 0 << ` `" "` `; ` ` ` `for` `(` `int` `j = 0; j <= maxSum + 1; j++) { ` ` ` `if` `(dp[j] == ` `true` `) ` ` ` `cout << j << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Function to find the total sum ` `// and print the distinct sum ` `void` `printDistinct(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `maxSum = 0; ` ` ` ` ` `// find the sum of array elements ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `maxSum += a[i]; ` ` ` `} ` ` ` ` ` `// Function to print all the distinct sum ` ` ` `subsetSum(a, n, maxSum); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 3, 4, 5, 6 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `printDistinct(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find total sum of ` `# all distinct subset sums in O(sum) space. ` ` ` `# Function to print all th distinct sum ` `def` `subsetSum(arr, n, maxSum): ` ` ` ` ` `# Declare a boolean array of size ` ` ` `# equal to total sum of the array ` ` ` `dp ` `=` `[` `False` `for` `i ` `in` `range` `(maxSum ` `+` `1` `)] ` ` ` ` ` `# Fill the first row beforehand ` ` ` `dp[arr[` `0` `]] ` `=` `True` ` ` ` ` `# dp[j] will be true only if sum j ` ` ` `# can be formed by any possible ` ` ` `# addition of numbers in given array ` ` ` `# upto index i, otherwise false ` ` ` `for` `i ` `in` `range` `(` `1` `, n, ` `1` `): ` ` ` ` ` `# Iterate from maxSum to 1 ` ` ` `# and avoid lookup on any other row ` ` ` `j ` `=` `maxSum ` ` ` `while` `(j >` `=` `1` `): ` ` ` ` ` `# Do not change the dp array ` ` ` `# for j less than arr[i] ` ` ` `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` ` ` ` ` `# If dp [j] is true then print ` ` ` `print` `(` `0` `, end ` `=` `" "` `) ` ` ` `for` `j ` `in` `range` `(maxSum ` `+` `1` `): ` ` ` `if` `(dp[j] ` `=` `=` `True` `): ` ` ` `print` `(j, end ` `=` `" "` `) ` ` ` `print` `(` `"21"` `) ` ` ` `# Function to find the total sum ` `# and print the distinct sum ` `def` `printDistinct(a, n): ` ` ` `maxSum ` `=` `0` ` ` ` ` `# find the sum of array elements ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `maxSum ` `+` `=` `a[i] ` ` ` ` ` `# Function to print all the distinct sum ` ` ` `subsetSum(a, n, maxSum) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `printDistinct(arr, n) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**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)

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.

## Recommended Posts:

- Find all distinct subset (or subsequence) sums of an array
- Sum of all subset sums of a linked list
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Partition a set into two subsets such that the difference of subset sums is minimum
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Find the equal pairs of subsequence of S and subsequence of T
- Maximum score assigned to a subsequence of numerically consecutive and distinct array elements
- Print all Strings from array A[] having all strings from array B[] as subsequence
- Find whether an array is subset of another array using Map
- Sum of the sums of all possible subsets
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Subset array sum by generating all the subsets
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Count distinct occurrences as a subsequence

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.