# Sum of the sums of all possible subsets

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.

**Time Complexity**: O(2^{N})

**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**as the contribution to the sum. Iterate through the array and add

^{(N-1)}**(arr[i] * 2**to the answer.

^{N-1})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); ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

20

**Time Complexity**: O(N)

**Space Complexity**: O(1)

**Note**: If N is large, the answer can overflow, thereby use larger data-type.

## Recommended Posts:

- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Minimum count of elements that sums to a given number
- K maximum sums of non-overlapping contiguous sub-arrays
- Maximum absolute difference of value and index sums
- K maximum sums of overlapping contiguous sub-arrays
- Possible two sets from first N natural numbers difference of sums as D
- Find k pairs with smallest sums in two arrays
- Count number of ordered pairs with Even and Odd Sums
- Smallest Integer to be inserted to have equal sums
- Count of pairs between two arrays such that the sums are distinct
- Subsets having Sum between A and B
- Sum of XOR of all possible subsets
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Find all distinct subset (or subsequence) sums of an array
- Find all distinct subset (or subsequence) sums of an array | Set-2

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.