Sum of subsets of all the subsets of an array | O ( N ) | To store factorial values ; Function to return ncr ; Function to return the required sum ; Initialising factorial ; Multiplier ; Finding the value of multipler according to the formula ; To store the final answer ; Calculate the final answer ; Driver code
let fact = new Array ( 10 ) ; function ncr ( n , r ) { return ( fact [ n ] / fact [ r ] ) / fact [ n - r ] ; } function findSum ( arr , n ) { fact [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; let mul = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) mul += Math . pow ( 2 , i ) * ncr ( n - 1 , i ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans += mul * arr [ i ] ; return ans ; } let arr = [ 1 , 1 ] ; let n = arr . length ; document . write ( findSum ( arr , n ) ) ;