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
< ? php function nCr ( $ n , $ k ) { $ C [ $ n + 1 ] [ $ k + 1 ] = 0 ; $ i ; $ j ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= 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 ) { $ result = 0.0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ sum += $ arr [ $ i ] ; for ( $ n = 1 ; $ n <= $ N ; $ n ++ ) $ result += ( ( $ sum * ( nCr ( $ N - 1 , $ n - 1 ) ) ) / $ n ) ; return $ result ; } $ arr = array ( 2 , 3 , 5 , 7 ) ; $ N = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo resultOfAllSubsets ( $ arr , $ N ) ; ? >