Sum of values of all possible non-empty subsets of the given array Last Updated : 13 Mar, 2022 Comments Improve Suggest changes Like Article Like Report 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 = 10Input: 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(N - 1). So, the required answer will be (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; } 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 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 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 JavaScript <script> // javascript implementation of the approach // Function to return the required sum function sum(arr, n) { // Find the sum of the array elements var sum = 0; for (i = 0; i < n; i++) { sum += arr[i]; } // Every element appears 2^(n-1) times sum = sum * parseInt(Math.pow(2, n - 1)); return sum; } // Driver code var arr = [ 2, 1, 5, 6 ]; var n = arr.length; document.write(sum(arr, n)); // This code is contributed by Amit Katiyar </script> Output: 112 Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Sum of values of all possible non-empty subsets of the given array S spp____ Follow Improve Article Tags : Mathematical DSA Arrays subset Practice Tags : ArraysMathematicalsubset Similar Reads Sum of products of all possible K size subsets of the given array Given an array arr[] of N non-negative integers and an integer 1 ? K ? N. The task is to find the sum of the products of all possible subsets of arr[] of size K. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: 35 (1 * 2) + (1 * 3) + (1 * 4) + (2 * 3) + (2 * 4) + (3 * 4) = 2 + 3 + 4 + 6 + 8 + 12 15+ 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 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 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 Largest value of K that a set of all possible subset-sum values of given Array contains numbers [0, K] Given an array arr[] of N integers, the task is to find the maximum count of K, i.e, consecutive integers from 0 to K, that is present in the set S, where S contains all the possible subset-sum values of the array arr[]. Examples: Input: arr[] = {1, 3}Output: 2Explanation: The possible subsets are { 6 min read Like