Given an array **arr[]** of **N** integers, the task is to find the sum of values of all possible non-empty subsets of array the given array.

**Examples:**

Input:arr[] = {2, 3}

Output:10

All non-empty subsets are {2}, {3} and {2, 3}

Total sum = 2 + 3 + 2 + 3 = 10

Input:arr[] = {2, 1, 5, 6}

Output:112

**Approach:** It can be observed that when all the elements are added from all the possible subsets then each element of the original array appears in **2 ^{(N – 1)}** times. Which means contribution of any element

**arr[i]**in the final answer will be

**arr[i] * 2**. So, the required answer will be

^{(N – 1)}**(arr[0] + arr[1] + arr[2] + … + arr[N – 1]) * 2**.

^{(N – 1)}Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the required sum ` `int` `sum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Find the sum of the array elements ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` ` ` `// Every element appears 2^(n-1) times ` ` ` `sum = sum * ` `pow` `(2, n - 1); ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 1, 5, 6 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << sum(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the required sum ` ` ` `static` `int` `sum(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Find the sum of the array elements ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` ` ` `// Every element appears 2^(n-1) times ` ` ` `sum = sum * (` `int` `)Math.pow(` `2` `, n - ` `1` `); ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `1` `, ` `5` `, ` `6` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(sum(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the required sum ` `def` `sum` `( arr, n): ` ` ` ` ` `# Find the sum of the array elements ` ` ` `sum` `=` `0` ` ` `for` `i ` `in` `arr : ` ` ` `sum` `+` `=` `i ` ` ` ` ` `# Every element appears 2^(n-1) times ` ` ` `sum` `=` `sum` `*` `pow` `(` `2` `, n ` `-` `1` `) ` ` ` `return` `sum` ` ` `# Driver code ` `arr ` `=` `[ ` `2` `, ` `1` `, ` `5` `, ` `6` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(` `sum` `(arr, n)) ` ` ` `# This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the required sum ` ` ` `static` `int` `sum(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Find the sum of the array elements ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` ` ` `// Every element appears 2^(n-1) times ` ` ` `sum = sum * (` `int` `)Math.Pow(2, n - 1); ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `[] arr = { 2, 1, 5, 6 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(sum(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by CodeMech ` |

*chevron_right*

*filter_none*

**Output:**

112

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:

- Product of values of all possible non-empty subsets of given Array
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- All possible values of floor(N/K) for all values of K
- Sum of products of all possible K size subsets of the given array
- Sum of bitwise OR of all possible subsets of given set
- Check if it is possible to split given Array into K odd-sum subsets
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Sum of the sums of all possible subsets
- Perfect Sum Problem (Print all subsets with given sum)
- Sum of squares of all Subsets of given Array
- Sum of cubes of all Subsets of given Array
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Number of subsets with same AND, OR and XOR values in an Array
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Sum of sum of all subsets of a set formed by first N natural numbers
- Recursive program to print all subsets with given sum
- Subset array sum by generating all the subsets

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.