Maximize the sum of sum of the Array by removing end elements | C # program to 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
using System ; class GFG { static int maxRemainingSum ( int [ ] arr , int n ) { int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; i = 0 ; int j = n - 1 ; int result = 0 ; while ( i < j ) { if ( arr [ i ] < arr [ j ] ) { sum -= arr [ i ] ; i ++ ; } else { sum -= arr [ j ] ; j -- ; } result += sum ; } return result ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 7 , 2 , 1 } ; int N = arr . Length ; Console . Write ( maxRemainingSum ( arr , N ) ) ; } }