Print sums of all subsets of a given set
Last Updated :
27 Feb, 2025
Given an array of integers, print sums of all subsets in it. Output sums can be printed in any order.
Examples :
Input: arr[] = {2, 3}
Output: 0 2 3 5
Explanation: All subsets of this array are - {{}, {2}, {3}, {2, 3}}, having sums - 0, 2, 3 and 5 respectively.
Input: arr[] = {2, 4, 5}
Output: 0 2 4 5 6 7 9 11
[Naive Approach] Using Iterative Method - O(N * 2^N) Time and O(1) Space
There are total 2n subsets. The idea is to generate a loop from 0 to 2n - 1. For every number, pick all array elements corresponding to 1s in the binary representation of the current number.
C++
// Iterative C++ program to print sums of all
// possible subsets.
#include <bits/stdc++.h>
using namespace std;
// Prints sums of all subsets of array
void subsetSums(vector<int> &arr, int n)
{
// There are total 2^n subsets
long long total = 1 << n;
// Consider all numbers from 0 to 2^n - 1
for (long long i = 0; i < total; i++) {
long long sum = 0;
// Consider binary representation of
// current i to decide which elements
// to pick.
for (int j = 0; j < n; j++)
if (i & (1 << j))
sum += arr[j];
// Print sum of picked elements.
cout << sum << " ";
}
}
// Driver code
int main()
{
vector<int> arr = { 5, 4, 3 };
int n = arr.size();
subsetSums(arr, n);
return 0;
}
Java
// Iterative Java program to print sums of all
// possible subsets.
import java.util.*;
class GFG {
// Prints sums of all subsets of array
static void subsetSums(int arr[], int n)
{
// There are total 2^n subsets
int total = 1 << n;
// Consider all numbers from 0 to 2^n - 1
for (int i = 0; i < total; i++) {
int sum = 0;
// Consider binary representation of
// current i to decide which elements
// to pick.
for (int j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
// Print sum of picked elements.
System.out.print(sum + " ");
}
}
// Driver code
public static void main(String args[])
{
int arr[] = new int[] { 5, 4, 3 };
int n = arr.length;
subsetSums(arr, n);
}
}
// This code is contributed by spp____
Python
# Iterative Python3 program to print sums of all possible subsets
# Prints sums of all subsets of array
def subsetSums(arr, n):
# There are total 2^n subsets
total = 1 << n
# Consider all numbers from 0 to 2^n - 1
for i in range(total):
Sum = 0
# Consider binary representation of
# current i to decide which elements
# to pick.
for j in range(n):
if ((i & (1 << j)) != 0):
Sum += arr[j]
# Print sum of picked elements.
print(Sum, "", end = "")
arr = [ 5, 4, 3 ]
n = len(arr)
subsetSums(arr, n);
# This code is contributed by mukesh07.
C#
// Iterative C# program to print sums of all
// possible subsets.
using System;
class GFG {
// Prints sums of all subsets of array
static void subsetSums(int[] arr, int n)
{
// There are total 2^n subsets
int total = 1 << n;
// Consider all numbers from 0 to 2^n - 1
for (int i = 0; i < total; i++) {
int sum = 0;
// Consider binary representation of
// current i to decide which elements
// to pick.
for (int j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
// Print sum of picked elements.
Console.Write(sum + " ");
}
}
static void Main() {
int[] arr = { 5, 4, 3 };
int n = arr.Length;
subsetSums(arr, n);
}
}
// This code is contributed by divyesh072019.
JavaScript
// Iterative Javascript program to print sums of all
// possible subsets.
// Prints sums of all subsets of array
function subsetSums(arr, n)
{
// There are total 2^n subsets
let total = 1 << n;
// Consider all numbers from 0 to 2^n - 1
for(let i = 0; i < total; i++)
{
let sum = 0;
// Consider binary representation of
// current i to decide which elements
// to pick.
for(let j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
// Print sum of picked elements.
process.stdout.write(sum + " ");
}
}
let arr = [ 5, 4, 3 ];
let n = arr.length;
subsetSums(arr, n);
Output :
0 5 4 9 3 8 7 12
[Expected Approach] Using Recursion - O(2^N) Time and O(N) Space
We can recursively solve this problem. There are total 2n subsets. For every element, we consider two choices, we include it in a subset and we don't include it in a subset. Below is recursive solution based on this idea.
C++
// C++ program to print sums of all possible
// subsets.
#include <bits/stdc++.h>
using namespace std;
// Prints sums of all subsets of arr[l..r]
void subsetSums(vector<int> &arr, int l, int r, int sum = 0)
{
// Print current subset
if (l > r) {
cout << sum << " ";
return;
}
// Subset including arr[l]
subsetSums(arr, l + 1, r, sum + arr[l]);
// Subset excluding arr[l]
subsetSums(arr, l + 1, r, sum);
}
// Driver code
int main()
{
vector<int> arr = { 5, 4, 3 };
int n = arr.size();
subsetSums(arr, 0, n - 1);
return 0;
}
Java
// Java program to print sums
// of all possible subsets.
import java.io.*;
class GFG {
// Prints sums of all
// subsets of arr[l..r]
static void subsetSums(int[] arr, int l, int r, int sum)
{
// Print current subset
if (l > r) {
System.out.print(sum + " ");
return;
}
// Subset including arr[l]
subsetSums(arr, l + 1, r, sum + arr[l]);
// Subset excluding arr[l]
subsetSums(arr, l + 1, r, sum);
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 5, 4, 3 };
int n = arr.length;
subsetSums(arr, 0, n - 1, 0);
}
}
// This code is contributed by anuj_67
Python
# Python3 program to print sums of
# all possible subsets.
# Prints sums of all subsets of arr[l..r]
def subsetSums(arr, l, r, sum=0):
# Print current subset
if l > r:
print(sum, end=" ")
return
# Subset including arr[l]
subsetSums(arr, l + 1, r, sum + arr[l])
# Subset excluding arr[l]
subsetSums(arr, l + 1, r, sum)
# Driver code
arr = [5, 4, 3]
n = len(arr)
subsetSums(arr, 0, n - 1)
# This code is contributed by Shreyanshi Arun.
C#
// C# program to print sums of all possible
// subsets.
using System;
class GFG {
// Prints sums of all subsets of
// arr[l..r]
static void subsetSums(int[] arr, int l, int r, int sum)
{
// Print current subset
if (l > r) {
Console.Write(sum + " ");
return;
}
// Subset including arr[l]
subsetSums(arr, l + 1, r, sum + arr[l]);
// Subset excluding arr[l]
subsetSums(arr, l + 1, r, sum);
}
// Driver code
public static void Main()
{
int[] arr = { 5, 4, 3 };
int n = arr.Length;
subsetSums(arr, 0, n - 1, 0);
}
}
// This code is contributed by anuj_67
JavaScript
// Javascript program to program to print
// sums of all possible subsets.
// Prints sums of all
// subsets of arr[l..r]
function subsetSums(arr, l, r, sum, result)
{
// Print current subset
if (l > r)
{
result.push(sum);
return;
}
// Subset including arr[l]
subsetSums(arr, l + 1, r,
sum + arr[l],result);
// Subset excluding arr[l]
subsetSums(arr, l + 1, r, sum,result);
}
// Driver code
let arr = [5, 4, 3];
let n = arr.length;
let result = [];
subsetSums(arr, 0, n - 1, 0,result);
console.log(result.join(" "));
// This code is contributed by code_hunt
Output :
12 9 8 5 7 4 3 0
[Alternate Approach] Using Iterative method - O(2^N) Time and O(N) Space
In this method, while visiting a new element, we take its sum with all previously stored sums. This method stores the sums of all subsets and hence it is valid for smaller inputs.
C++
// Iterative C++ program to print sums of all
// possible subsets.
#include <bits/stdc++.h>
using namespace std;
// Prints sums of all subsets of array
void subsetSums(vector<int> &nums, int n)
{
// There are total 2^n subsets
vector<int> s = {0};//store the sums
for (int i = 0; i <n; i++) {
const int v = s.size();
for (int t = 0; t < v; t++) {
s.push_back(s[t] + nums[i]); //add this element with previous subsets
}
}
// Print
for(int i=0;i<s.size();i++)
cout << s[i] << " ";
}
// Driver code
int main()
{
vector<int> arr = { 5, 4, 3 };
int n = arr.size();
subsetSums(arr, n);
return 0;
}
Java
import java.util.ArrayList;
public class Main {
public static void subsetSums(int[] nums, int n) {
ArrayList<Integer> s = new ArrayList<Integer>();
s.add(0);
for (int i = 0; i < n; i++) {
int v = s.size();
for (int t = 0; t < v; t++) {
s.add(s.get(t) + nums[i]);
}
}
for (int i = 0; i < s.size(); i++) {
System.out.print(s.get(i) + " ");
}
}
public static void main(String[] args) {
int[] arr = { 5, 4, 3 };
int n = arr.length;
subsetSums(arr, n);
}
}
Python
# Iterative Python program to print sums of all
# possible subsets.
# Prints sums of all subsets of array
def subsetSums(nums,n):
# There are total 2^n subsets
s = [0]
for i in range(n):
v = len(s)
for t in range(v):
s.append(s[t] + nums[i]) # add this element with previous subsets
# Print
for i in s:
print(i, end=" ")
# Driver code
arr = [5, 4, 3 ]
n = len(arr)
subsetSums(arr, n)
C#
// C# program to print sums of all possible subsets
using System;
public class Subsets
{
static void subsetSums(int[] nums, int n)
{
// There are total 2^n subsets
int[] s = new int[1];
s[0] = 0;
for (int i = 0; i < n; i++)
{
int v = s.Length;
for (int t = 0; t < v; t++)
{
// add the current element with all previous subsets
Array.Resize(ref s, s.Length + 1); // increase the size of the array
s[s.Length - 1] = s[t] + nums[i]; // add the current element with previous subsets
}
}
// Print
Console.Write(string.Join(" ", s));
}public static void Main()
{
int[] arr = { 5, 4, 3 };
int n = arr.Length;
subsetSums(arr, n);
}
}
JavaScript
// Iterative JavaScript program to print sums of all possible subsets
function subsetSums(nums, n) {
// There are total 2^n subsets
let s = [0];
for (let i = 0; i < n; i++) {
let v = s.length;
for (let t = 0; t < v; t++) {
s.push(s[t] + nums[i]); // add this element with previous subsets
}
}
// Print
for (let i=0; i<s.length; i++) {
process.stdout.write(s[i]+" ");
}
}
// Driver code
let arr = [5, 4, 3];
let n = arr.length;
subsetSums(arr, n);
Output:
0 5 4 9 3 8 7 12
The above-mentioned techniques can be used to perform various operations on sub-sets like multiplication, division, XOR, OR, etc, without actually creating and storing the sub-sets and thus making the program memory efficient.
Similar Reads
Print all subsets of given size of a set Generate all possible subsets of size r of the given array with distinct elements. Examples: Input : arr[] = {1, 2, 3, 4} r = 2Output : 1 2 1 3 1 4 2 3 2 4 3 4Input : arr[] = {10, 20, 30, 40, 50} r = 3Output : 10 20 30 10 20 40 10 20 50 10 30 40 10 30 50 10 40 50 20 30 40 20 30 50 20 40 50 30 40 50
15+ min read
Print all subsets of a given Set or Array Given an array arr of size n, your task is to print all the subsets of the array in lexicographical order.A subset is any selection of elements from an array, where the order does not matter, and no element appears more than once. It can include any number of elements, from none (the empty subset) t
12 min read
Sum of all subsets of a given size (=K) Given an array arr[] consisting of N integers and a positive integer K, the task is to find the sum of all the subsets of size K. Examples: Input: arr[] = {1, 2, 4, 5}, K = 2Output: 36Explanation:The subsets of size K(= 2) are = {1, 2}, {1, 4}, {1, 5}, {2, 4}, {2, 5}, {4, 5}. Now, the sum of all sub
7 min read
Print all submasks of a given mask Given an integer N, the task is to print all the subsets of the set formed by the set bits present in the binary representation of N. Examples: Input: N = 5Output: 5 4 1 Explanation:Binary representation of N is "101", Therefore all the required subsets are {"101", "100", "001", "000"}. Input: N = 2
4 min read
Find all Unique Subsets of a given Set Given an array A[] of positive integers, print all the unique non-empty subsets of the array Note: The set can not contain duplicate elements, so any repeated subset should be considered only once in the output. Examples: Input: A[] = {1, 5, 6}Output: {{1}, {1, 5}, {1, 6}, {5}, {5, 6}, {6}, {1, 5,
15+ min read
Sum of bitwise AND of all possible subsets of given set Given an array, we need to calculate the Sum of Bit-wise AND of all possible subsets of the given array. Examples: Input : 1 2 3 Output : 9 For [1, 2, 3], all possible subsets are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} Bitwise AND of these subsets are, 1 + 2 + 3 + 0 + 1 + 2 + 0 = 9. So, th
7 min read