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
#include <iostream> NEW_LINE using namespace std ; int maxRemainingSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int 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 ; } int main ( ) { int arr [ ] = { 3 , 1 , 7 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxRemainingSum ( arr , N ) ; return 0 ; }