Sum of subsets of all the subsets of an array | O ( 3 ^ N ) | C # implementation of the approach ; To store the readonly ans ; Function to sum of all subsets of a given array ; Base case ; Recursively calling subsetSum ; 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 List < int > c = new List < int > ( ) ; static int ans = 0 ; static void subsetSum ( int i , int curr ) { if ( i == c . Count ) { ans += curr ; return ; } subsetSum ( i + 1 , curr + c [ i ] ) ; subsetSum ( i + 1 , curr ) ; } static void subsetGen ( int [ ] arr , int i , int n ) { if ( i == n ) { subsetSum ( 0 , 0 ) ; return ; } subsetGen ( arr , i + 1 , n ) ; c . Add ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n ) ; c . RemoveAt ( c . Count - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; int n = arr . Length ; subsetGen ( arr , 0 , n ) ; Console . WriteLine ( ans ) ; } }