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
def maxRemainingSum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT i = 0 NEW_LINE j = n - 1 NEW_LINE result = 0 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( arr [ i ] < arr [ j ] ) : NEW_LINE INDENT sum -= arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum -= arr [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT result += sum ; NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 3 , 1 , 7 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxRemainingSum ( arr , N ) ) NEW_LINE