Sum of subsets of all the subsets of an array | O ( 2 ^ N ) | 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
var ans = 0 ; function subsetSum ( c ) { var L = c . length ; var mul = parseInt ( Math . pow ( 2 , L - 1 ) ) ; for ( i = 0 ; i < c . length ; i ++ ) ans += c [ i ] * mul ; } function subsetGen ( arr , i , n , c ) { if ( i == n ) { subsetSum ( c ) ; return ; } subsetGen ( arr , i + 1 , n , c ) ; c . push ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , c ) ; c . pop ( 0 ) ; } var arr = [ 1 , 1 ] ; var n = arr . length ; var c = [ ] ; subsetGen ( arr , 0 , n , c ) ; document . write ( ans ) ;