Sum of products of all combination taken ( 1 to n ) at a time | find the postfix sum array ; modify the array such that we don 't  have to compute the products which  are obtained before ; finding sum of all combination taken 1 to N at a time ; sum taken 1 at time is simply sum of 1 - N ; for sum of products for all combination ; finding postfix array ; sum of products taken i + 1 at a time ; modify the array for overlapping problem ; Driver 's Code ; storing numbers from 1 to N ; calling allCombination
function postfix ( a , n ) { for ( let i = n - 1 ; i > 0 ; i -- ) { a [ i - 1 ] = a [ i - 1 ] + a [ i ] ; } } function modify ( a , n ) { for ( let i = 1 ; i < n ; i ++ ) { a [ i - 1 ] = i * a [ i ] ; } } function allCombination ( a , n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { sum += i ; } document . write ( " " + sum + " " ) ; for ( let i = 1 ; i < n ; i ++ ) { postfix ( a , n - i + 1 ) ; sum = 0 ; for ( let j = 1 ; j <= n - i ; j ++ ) { sum += ( j * a [ j ] ) ; } document . write ( " " + ( i + 1 ) + " " + sum + " " ) ; modify ( a , n ) ; } } let n = 5 ; let a = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { a [ i ] = i + 1 ; } allCombination ( a , n ) ;