# Maximize the maximum subarray sum after removing atmost one element

Given an array arr[] of N integers. The task is to first find the maximum sub-array sum and then remove at most one element from the sub-array. If there are multiple sub-arrays with the maximum sub-array sum then remove at most a single element such that the maximum sum after removal is maximized. The task is to maximize the sum obtained after removal.

Note: You have to first find the maximum sub-array sum and then remove the element from that sub-array if necessary. Also after removal, the sub-array size should at least be 1.

Examples:

Input: arr[] = {1, 2, 3, -2, 3}
Output: 9
The maximum sub-array sum is given by the sub-array {2, 3, -2, 3}
Hence, we can remove -2 to further maximize the sub-array sum.

Input: arr[] = {-1, -2}
Output: -1
The maximum sub-array sum is from the sub-array {-1} and no removal is required.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Use Kadane’s algorithm to find the maximum subarray sum. Once the sum has been find, re-apply Kadane’s algorithm to find the maximum sum again with some minor changes. Use two extra variables in the loop, cnt, and mini. The variable cnt counts the number of elements in sub-array and mini stores the minimum value in all the sub-arrays which have same sum as maximum sub-array. If the minimum element thus obtained is less than 0, then only we remove an element from the subarray, or else we donot.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the maximum sub-array sum ` `int` `maxSubArraySum(``int` `a[], ``int` `size) ` `{ ` ` `  `    ``// Initialized ` `    ``int` `max_so_far = INT_MIN, max_ending_here = 0; ` ` `  `    ``// Traverse in the array ` `    ``for` `(``int` `i = 0; i < size; i++) { ` ` `  `        ``// Increase the sum ` `        ``max_ending_here = max_ending_here + a[i]; ` ` `  `        ``// If sub-array sum is more than the previous ` `        ``if` `(max_so_far < max_ending_here) ` `            ``max_so_far = max_ending_here; ` ` `  `        ``// If sum is negative ` `        ``if` `(max_ending_here < 0) ` `            ``max_ending_here = 0; ` `    ``} ` `    ``return` `max_so_far; ` `} ` ` `  `// Function that returns the maximum sub-array sum ` `// after removing an element from the same sub-array ` `int` `maximizeSum(``int` `a[], ``int` `n) ` `{ ` `    ``int` `cnt = 0; ` `    ``int` `mini = INT_MAX; ` `    ``int` `minSubarray = INT_MAX; ` ` `  `    ``// Maximum sub-array sum using Kadane's Algorithm ` `    ``int` `sum = maxSubArraySum(a, n); ` ` `  `    ``int` `max_so_far = INT_MIN, max_ending_here = 0; ` ` `  `    ``// Re-apply Kadane's with minor changes ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Increase the sum ` `        ``max_ending_here = max_ending_here + a[i]; ` `        ``cnt++; ` `        ``minSubarray = min(a[i], minSubarray); ` ` `  `        ``// If sub-array sum is greater than the previous ` `        ``if` `(sum == max_ending_here) { ` ` `  `            ``// If elements are 0, no removal ` `            ``if` `(cnt == 1) ` `                ``mini = min(mini, 0); ` ` `  `            ``// If elements are more, then store ` `            ``// the minimum value in the sub-array ` `            ``// obtained till now ` `            ``else` `                ``mini = min(mini, minSubarray); ` `        ``} ` ` `  `        ``// If sum is negative ` `        ``if` `(max_ending_here < 0) { ` ` `  `            ``// Re-initialize everything ` `            ``max_ending_here = 0; ` `            ``cnt = 0; ` `            ``minSubarray = INT_MAX; ` `        ``} ` `    ``} ` ` `  `    ``return` `sum - mini; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 2, 3, -2, 3 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``cout << maximizeSum(a, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` `     `  `// Function to return the maximum sub-array sum ` `static` `int` `maxSubArraySum(``int` `a[], ``int` `size) ` `{ ` ` `  `    ``// Initialized ` `    ``int` `max_so_far = Integer.MIN_VALUE,  ` `        ``max_ending_here = ``0``; ` ` `  `    ``// Traverse in the array ` `    ``for` `(``int` `i = ``0``; i < size; i++) ` `    ``{ ` ` `  `        ``// Increase the sum ` `        ``max_ending_here = max_ending_here + a[i]; ` ` `  `        ``// If sub-array sum is more than the previous ` `        ``if` `(max_so_far < max_ending_here) ` `            ``max_so_far = max_ending_here; ` ` `  `        ``// If sum is negative ` `        ``if` `(max_ending_here < ``0``) ` `            ``max_ending_here = ``0``; ` `    ``} ` `    ``return` `max_so_far; ` `} ` ` `  `// Function that returns the maximum sub-array sum ` `// after removing an element from the same sub-array ` `static` `int` `maximizeSum(``int` `a[], ``int` `n) ` `{ ` `    ``int` `cnt = ``0``; ` `    ``int` `mini = Integer.MAX_VALUE; ` `    ``int` `minSubarray = Integer.MAX_VALUE; ` ` `  `    ``// Maximum sub-array sum  ` `    ``// using Kadane's Algorithm ` `    ``int` `sum = maxSubArraySum(a, n); ` ` `  `    ``int` `max_so_far = Integer.MIN_VALUE,  ` `        ``max_ending_here = ``0``; ` ` `  `    ``// Re-apply Kadane's with minor changes ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` ` `  `        ``// Increase the sum ` `        ``max_ending_here = max_ending_here + a[i]; ` `        ``cnt++; ` `        ``minSubarray = Math.min(a[i], minSubarray); ` ` `  `        ``// If sub-array sum is greater than the previous ` `        ``if` `(sum == max_ending_here) ` `        ``{ ` ` `  `            ``// If elements are 0, no removal ` `            ``if` `(cnt == ``1``) ` `                ``mini = Math.min(mini, ``0``); ` ` `  `            ``// If elements are more, then store ` `            ``// the minimum value in the sub-array ` `            ``// obtained till now ` `            ``else` `                ``mini = Math.min(mini, minSubarray); ` `        ``} ` ` `  `        ``// If sum is negative ` `        ``if` `(max_ending_here < ``0``) ` `        ``{ ` ` `  `            ``// Re-initialize everything ` `            ``max_ending_here = ``0``; ` `            ``cnt = ``0``; ` `            ``minSubarray = Integer.MAX_VALUE; ` `        ``} ` `    ``} ` ` `  `    ``return` `sum - mini; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `a[] = { ``1``, ``2``, ``3``, -``2``, ``3` `}; ` `    ``int` `n = a.length; ` `    ``System.out.println(maximizeSum(a, n)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

## Python3

 `# Python3 implementation of the approach  ` `import` `sys; ` ` `  `# Function to return the maximum sub-array sum  ` `def` `maxSubArraySum(a, size) :  ` ` `  `    ``# Initialized  ` `    ``max_so_far ``=` `-``(sys.maxsize ``-` `1``); ` `    ``max_ending_here ``=` `0``;  ` ` `  `    ``# Traverse in the array  ` `    ``for` `i ``in` `range``(size) : ` ` `  `        ``# Increase the sum  ` `        ``max_ending_here ``=` `max_ending_here ``+` `a[i];  ` ` `  `        ``# If sub-array sum is more than the previous  ` `        ``if` `(max_so_far < max_ending_here) : ` `            ``max_so_far ``=` `max_ending_here;  ` ` `  `        ``# If sum is negative  ` `        ``if` `(max_ending_here < ``0``) : ` `            ``max_ending_here ``=` `0``;  ` `     `  `    ``return` `max_so_far;  ` ` `  `# Function that returns the maximum  ` `# sub-array sum after removing an  ` `# element from the same sub-array  ` `def` `maximizeSum(a, n) :  ` ` `  `    ``cnt ``=` `0``;  ` `    ``mini ``=` `sys.maxsize; ` `    ``minSubarray ``=` `sys.maxsize;  ` ` `  `    ``# Maximum sub-array sum using ` `    ``# Kadane's Algorithm  ` `    ``sum` `=` `maxSubArraySum(a, n);  ` ` `  `    ``max_so_far ``=` `-``(sys.maxsize ``-` `1``); ` `    ``max_ending_here ``=` `0``;  ` ` `  `    ``# Re-apply Kadane's with minor changes  ` `    ``for` `i ``in` `range``(n) : ` ` `  `        ``# Increase the sum  ` `        ``max_ending_here ``=` `max_ending_here ``+` `a[i];  ` `        ``cnt ``+``=` `1``;  ` `        ``minSubarray ``=` `min``(a[i], minSubarray);  ` ` `  `        ``# If sub-array sum is greater  ` `        ``# than the previous  ` `        ``if` `(``sum` `=``=` `max_ending_here) : ` ` `  `            ``# If elements are 0, no removal  ` `            ``if` `(cnt ``=``=` `1``) : ` `                ``mini ``=` `min``(mini, ``0``);  ` ` `  `            ``# If elements are more, then store  ` `            ``# the minimum value in the sub-array  ` `            ``# obtained till now  ` `            ``else` `: ` `                ``mini ``=` `min``(mini, minSubarray);  ` `         `  `        ``# If sum is negative  ` `        ``if` `(max_ending_here < ``0``) : ` ` `  `            ``# Re-initialize everything  ` `            ``max_ending_here ``=` `0``;  ` `            ``cnt ``=` `0``;  ` `            ``minSubarray ``=` `sys.maxsize;  ` ` `  `    ``return` `sum` `-` `mini;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``a ``=` `[ ``1``, ``2``, ``3``, ``-``2``, ``3` `];  ` `    ``n ``=` `len``(a) ` `     `  `    ``print``(maximizeSum(a, n));  ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the maximum sub-array sum ` `static` `int` `maxSubArraySum(``int` `[]a, ``int` `size) ` `{ ` ` `  `    ``// Initialized ` `    ``int` `max_so_far = ``int``.MinValue,  ` `        ``max_ending_here = 0; ` ` `  `    ``// Traverse in the array ` `    ``for` `(``int` `i = 0; i < size; i++) ` `    ``{ ` ` `  `        ``// Increase the sum ` `        ``max_ending_here = max_ending_here + a[i]; ` ` `  `        ``// If sub-array sum is more than the previous ` `        ``if` `(max_so_far < max_ending_here) ` `            ``max_so_far = max_ending_here; ` ` `  `        ``// If sum is negative ` `        ``if` `(max_ending_here < 0) ` `            ``max_ending_here = 0; ` `    ``} ` `    ``return` `max_so_far; ` `} ` ` `  `// Function that returns the maximum sub-array sum ` `// after removing an element from the same sub-array ` `static` `int` `maximizeSum(``int` `[]a, ``int` `n) ` `{ ` `    ``int` `cnt = 0; ` `    ``int` `mini = ``int``.MaxValue; ` `    ``int` `minSubarray = ``int``.MaxValue; ` ` `  `    ``// Maximum sub-array sum  ` `    ``// using Kadane's Algorithm ` `    ``int` `sum = maxSubArraySum(a, n); ` ` `  `    ``int` `max_so_far = ``int``.MinValue,  ` `        ``max_ending_here = 0; ` ` `  `    ``// Re-apply Kadane's with minor changes ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` ` `  `        ``// Increase the sum ` `        ``max_ending_here = max_ending_here + a[i]; ` `        ``cnt++; ` `        ``minSubarray = Math.Min(a[i], minSubarray); ` ` `  `        ``// If sub-array sum is greater than the previous ` `        ``if` `(sum == max_ending_here) ` `        ``{ ` ` `  `            ``// If elements are 0, no removal ` `            ``if` `(cnt == 1) ` `                ``mini = Math.Min(mini, 0); ` ` `  `            ``// If elements are more, then store ` `            ``// the minimum value in the sub-array ` `            ``// obtained till now ` `            ``else` `                ``mini = Math.Min(mini, minSubarray); ` `        ``} ` ` `  `        ``// If sum is negative ` `        ``if` `(max_ending_here < 0) ` `        ``{ ` ` `  `            ``// Re-initialize everything ` `            ``max_ending_here = 0; ` `            ``cnt = 0; ` `            ``minSubarray = ``int``.MaxValue; ` `        ``} ` `    ``} ` ` `  `    ``return` `sum - mini; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]a = { 1, 2, 3, -2, 3 }; ` `    ``int` `n = a.Length; ` `    ``Console.WriteLine(maximizeSum(a, n)); ` `} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

## PHP

 ` `

Output:

```9
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.