Sum of all Subarrays | Set 1 | Function compute sum all sub - array ; Computing sum of subarray using formula ; Return all subarray sum ; Driver code
function SubArraySum ( arr , n ) { let result = 0 ; for ( let i = 0 ; i < n ; i ++ ) result += ( arr [ i ] * ( i + 1 ) * ( n - i ) ) ; return result ; } let arr = [ 1 , 2 , 3 ] ; let n = arr . length ; document . write ( " " + SubArraySum ( arr , n ) ) ;