Sum of subsets of all the subsets of an array | O ( 2 ^ N ) | C # implementation of the approach ; To store the final ans ; Function to sum of all subsets of a given array ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int ans ; static void subsetSum ( List < int > c ) { int L = c . Count ; int mul = ( int ) Math . Pow ( 2 , L - 1 ) ; for ( int i = 0 ; i < c . Count ; i ++ ) ans += c [ i ] * mul ; } static void subsetGen ( int [ ] arr , int i , int n , List < int > c ) { if ( i == n ) { subsetSum ( c ) ; return ; } subsetGen ( arr , i + 1 , n , c ) ; c . Add ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , c ) ; c . RemoveAt ( 0 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; int n = arr . Length ; List < int > c = new List < int > ( ) ; subsetGen ( arr , 0 , n , c ) ; Console . WriteLine ( ans ) ; } }