Sum of the sums of all possible subsets
Last Updated :
28 Dec, 2022
Given an array a of size N. The task is to find the sum of the sums of all possible subsets.
Examples:
Input: a[] = {3, 7}
Output: 20
The subsets are: {3} {7} {3, 7}
{3, 7} = 10
{3} = 3
{7} = 7
10 + 3 + 7 = 20
Input: a[] = {10, 16, 14, 9}
Output: 392
Naive Approach: A naive approach is to find all the subsets using power set and then summate all the possible subsets to get the answer.
C++
// C++ program to check if there is a subset
// with sum divisible by m.
#include <bits/stdc++.h>
using namespace std;
int helper(int N, int nums[], int sum, int idx)
{
// if we reach last index
if (idx == N) {
// and if the sum mod m is zero
return sum;
}
// 2 choices - to pick or to not pick
int picked = helper(N, nums, sum + nums[idx], idx + 1);
int notPicked = helper(N, nums, sum, idx + 1);
return picked + notPicked;
}
int sumOfSubset(int arr[], int n)
{
return helper(n, arr, 0, 0);
}
// Driver code
int main()
{
int arr[] = { 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << sumOfSubset(arr, n);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static int helper(int N, int nums[], int sum, int idx)
{
// if we reach last index
if (idx == N) {
// and if the sum mod m is zero
return sum;
}
// 2 choices - to pick or to not pick
int picked
= helper(N, nums, sum + nums[idx], idx + 1);
int notPicked = helper(N, nums, sum, idx + 1);
return picked + notPicked;
}
static int sumOfSubset(int arr[], int n)
{
return helper(n, arr, 0, 0);
}
public static void main(String[] args)
{
int arr[] = { 3, 7 };
int n = arr.length;
System.out.println(sumOfSubset(arr, n));
}
}
// This code is contributed by aadityaburujwale.
Python3
# Python program to check if there is a subset
# with sum divisible by m.
def helper(N, nums, sum, idx):
# if we reach last index
if idx == N:
# and if the sum mod m is zero
return sum
# 2 choices - to pick or to not pick
picked = helper(N, nums, sum + nums[idx], idx + 1)
not_picked = helper(N, nums, sum, idx + 1)
return picked + not_picked
def sum_of_subset(arr, n):
return helper(n, arr, 0, 0)
# Test the program
arr = [3, 7]
n = len(arr)
print(sum_of_subset(arr, n))
# This code is contributed by divyansh2212
C#
/*package whatever //do not write package name here */
using System;
class GFG {
static int helper(int N, int[] nums, int sum, int idx)
{
// if we reach last index
if (idx == N) {
// and if the sum mod m is zero
return sum;
}
// 2 choices - to pick or to not pick
int picked
= helper(N, nums, sum + nums[idx], idx + 1);
int notPicked = helper(N, nums, sum, idx + 1);
return picked + notPicked;
}
static int sumOfSubset(int[] arr, int n)
{
return helper(n, arr, 0, 0);
}
public static void Main()
{
int[] arr = { 3, 7 };
int n = arr.Length;
Console.WriteLine(sumOfSubset(arr, n));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
// JS program to check if there is a subset
// with sum divisible by m.
function helper(N, nums, sum, idx)
{
// if we reach last index
if (idx == N)
{
// and if the sum mod m is zero
return sum;
}
// 2 choices - to pick or to not pick
let picked = helper(N, nums, sum + nums[idx], idx + 1);
let notPicked = helper(N, nums, sum, idx + 1);
return picked + notPicked;
}
function sumOfSubset(arr, n)
{
return helper(n, arr, 0, 0);
}
// Driver code
let arr = [ 3, 7 ];
let n = arr.length;
console.log(sumOfSubset(arr, n));
// This code is contributed by akashish__
Time Complexity: O(2N)
Space Complexity: O(N) because of Recursion Stack Space
Efficient Approach: An efficient approach is to solve the problem using observation. If we write all the subsequences, a common point of observation is that each number appears 2(N - 1) times in a subset and hence will lead to the 2(N-1) as the contribution to the sum. Iterate through the array and add (arr[i] * 2N-1) to the answer.
Below is the implementation of the above approach:
C++
// C++ program to find the sum of
// the addition of all possible subsets.
#include <bits/stdc++.h>
using namespace std;
// Function to find the sum
// of sum of all the subset
int sumOfSubset(int a[], int n)
{
int times = pow(2, n - 1);
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
int main()
{
int a[] = { 3, 7 };
int n = sizeof(a) / sizeof(a[0]);
cout << sumOfSubset(a, n);
}
Java
// Java program to find the sum of
// the addition of all possible subsets.
class GFG
{
// Function to find the sum
// of sum of all the subset
static int sumOfSubset(int []a, int n)
{
int times = (int)Math.pow(2, n - 1);
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
int []a = { 3, 7 };
int n = a.length;
System.out.println(sumOfSubset(a, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the Sum of
# the addition of all possible subsets.
# Function to find the sum
# of sum of all the subset
def SumOfSubset(a, n):
times = pow(2, n - 1)
Sum = 0
for i in range(n):
Sum = Sum + (a[i] * times)
return Sum
# Driver Code
a = [3, 7]
n = len(a)
print(SumOfSubset(a, n))
# This code is contributed by Mohit Kumar
C#
// C# program to find the sum of
// the addition of all possible subsets.
using System;
class GFG
{
// Function to find the sum
// of sum of all the subset
static int sumOfSubset(int []a, int n)
{
int times = (int)Math.Pow(2, n - 1);
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
public static void Main()
{
int []a = { 3, 7 };
int n = a.Length;
Console.Write(sumOfSubset(a, n));
}
}
// This code is contributed by Nidhi
JavaScript
<script>
// javascript program to find the sum of
// the addition of all possible subsets.
// Function to find the sum
// of sum of all the subset
function sumOfSubset(a , n) {
var times = parseInt( Math.pow(2, n - 1));
var sum = 0;
for (i = 0; i < n; i++) {
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
var a = [ 3, 7 ];
var n = a.length;
document.write(sumOfSubset(a, n));
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(N)
Space Complexity: O(1)
Note: If N is large, the answer can overflow, thereby use larger data-type.
Similar Reads
Sum of XOR of all possible subsets Given an array of size n, the task is to find the sum of all the values that come from XORing all the elements of the subsets. Examples:Input: arr[] = [1, 2, 3]Output: 28Explanation: Subsets are: [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]0(empty subset)1 = 12 = 23 = 31 ^ 2 = 31 ^ 3 = 22
11 min read
Sum of the products of all possible Subsets Given an array of n non-negative integers. The task is to find the sum of the product of elements of all the possible subsets. It may be assumed that the numbers in subsets are small and computing product doesn't cause arithmetic overflow. Example : Input : arr[] = {1, 2, 3} Output : 23 Possible Sub
4 min read
Sum of subsets of all the subsets of an array | O(N) Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset wi
7 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
Sum of subsets of all the subsets of an array | O(3^N) Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset wi
6 min read
Sum of subsets of all the subsets of an array | O(2^N) Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset wi
6 min read