Sum of all Subarrays | Set 1 | Computes sum all sub - array ; Pick starting point ; Pick ending point ; sum subarray between current starting and ending points ; driver program to test above function
function SubArraySum ( arr , n ) { let result = 0 , temp = 0 ; for ( let i = 0 ; i < n ; i ++ ) { temp = 0 ; for ( let j = i ; j < n ; j ++ ) { temp += arr [ j ] ; result += temp ; } } return result ; } let arr = [ 1 , 2 , 3 ] ; let n = arr . length ; document . write ( " " + SubArraySum ( arr , n ) + " " ) ;