Sum of average of all subsets | Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; method returns sum of average of all subsets ; Initialize result ; Find sum of elements ; looping once for all subset of same size ; each element occurs nCr ( N - 1 , n - 1 ) times while considering subset of size n ; Driver code to test above methods
function nCr ( n , k ) { let C = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { C [ i ] = new Array ( k + 1 ) ; for ( let j = 0 ; j <= k ; j ++ ) { C [ i ] [ j ] = 0 ; } } let i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } function resultOfAllSubsets ( arr , N ) { let result = 0.0 ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; for ( let n = 1 ; n <= N ; n ++ ) result += ( sum * ( nCr ( N - 1 , n - 1 ) ) ) / n ; return result ; } let arr = [ 2 , 3 , 5 , 7 ] ; let N = arr . length ; document . write ( resultOfAllSubsets ( arr , N ) ) ;