12. Maximum Subarray Sum with One Deletion
Previous11. Maximum Number of Non-Overlapping Subarrays With Sum Equals TargetNext13. Shortest Subarray with Sum at Least K (Negative Numbers)
Last updated
Last updated
Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible.
Note that the subarray needs to be non-empty after deleting one element.
Example 1:
Example 2:
Example 3:
Approach:
Getting current maximum and updation is same as Kadane’s algorithm. Now when both arrays are created, we can use them for one element removal conditions as follows, at each index i, maximum subarray sum after ignoring i’th element will be fw[i-1] + bw[i+1]
Time Complexity:O(n)