Maximize the sum of sum of the Array by removing end elements | Function to find the maximum sum of sum ; compute the sum of whole array ; Traverse and remove the minimum value from an end to maximum the sum value ; If the left end element is smaller than right end ; remove the left end element ; If the right end element is smaller than left end ; remove the right end element ; Add the remaining element sum in the result ; Return the maximum sum of sum ; Driver code
function maxRemainingSum ( arr , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; var i = 0 ; var j = n - 1 ; var result = 0 ; while ( i < j ) { if ( arr [ i ] < arr [ j ] ) { sum -= arr [ i ] ; i ++ ; } else { sum -= arr [ j ] ; j -- ; } result += sum ; } return result ; } var arr = [ 3 , 1 , 7 , 2 , 1 ] ; var N = arr . length ; document . write ( maxRemainingSum ( arr , N ) ) ;