Sum of subsets of all the subsets of an array | O ( 3 ^ N ) | Javascript implementation of the approach ; To store the final 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
var ans = 0 ; var c = [ ] ; var ans = 0 ; var c = [ ] ; subsetGen ( arr , 0 , n , ans ) ; document . write ( ans ) ; function subsetSum ( i , curr ) { if ( i == c . length ) { ans += curr ; return ; } subsetSum ( i + 1 , curr + c [ i ] ) ; subsetSum ( i + 1 , curr ) ; } function subsetGen ( arr , i , n , ans ) { if ( i == n ) { subsetSum ( 0 , ans , 0 ) ; return ; } subsetGen ( arr , i + 1 , n , ans ) ; c . push ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , ans ) ; c . pop ( ) ; } var arr = [ 1 , 1 ] ; var n = arr . length ;