Sum of subsets of all the subsets of an array | O ( 2 ^ N ) | store the answer ; 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
c = [ ] NEW_LINE ans = 0 NEW_LINE def subsetSum ( ) : NEW_LINE INDENT global ans NEW_LINE L = len ( c ) NEW_LINE mul = pow ( 2 , L - 1 ) NEW_LINE i = 0 NEW_LINE while ( i < len ( c ) ) : NEW_LINE INDENT ans += c [ i ] * mul NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def subsetGen ( arr , i , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT subsetSum ( ) NEW_LINE return NEW_LINE DEDENT subsetGen ( arr , i + 1 , n ) NEW_LINE c . append ( arr [ i ] ) NEW_LINE subsetGen ( arr , i + 1 , n ) NEW_LINE c . pop ( ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE subsetGen ( arr , 0 , n ) NEW_LINE print ( ans ) NEW_LINE DEDENT