Sum of products of all possible Subarrays | Function that finds the sum of products of all subarray of arr [ ] ; Stores sum of all subarrays ; Iterate array from behind ; Update the ans ; Update the res ; Prlet the final sum ; Given array arr [ ] ; Size of array ; Function Call
function sumOfSubarrayProd ( arr , n ) { let ans = 0 ; let res = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { let incr = arr [ i ] * ( 1 + res ) ; ans += incr ; res = incr ; } document . write ( ans ) ; } let arr = [ 1 , 2 , 3 ] ; let N = arr . length ; sumOfSubarrayProd ( arr , N ) ;