Sum of the updated array after performing the given operation | Utility function to return the sum of the array ; Function to return the sum of the modified array ; Subtract the subarray sum ; Sum of subarray arr [ i ... n - 1 ] ; Return the sum of the modified array ; Driver code
function sumArr ( arr , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } function sumModArr ( arr , n ) { var subSum = arr [ n - 1 ] ; for ( var i = n - 2 ; i >= 0 ; i -- ) { var curr = arr [ i ] ; arr [ i ] -= subSum ; subSum += curr ; } return sumArr ( arr , n ) ; } var arr = [ 40 , 25 , 12 , 10 ] ; var n = arr . length ; document . write ( sumModArr ( arr , n ) ) ;