Problem

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.

Examples

Example 1

1
2
3
Input: arr = [1,-2,0,3]
Output: 4
Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.

Example 2

1
2
3
Input: arr = [1,-2,-2,3]
Output: 3
Explanation: We just choose [3] and it's the maximum sum.

Example 3

1
2
3
Input: arr = [-1,-1,-1,-1]
Output: -1
Explanation: The final subarray needs to be non-empty. You can't choose [-1] and delete -1 from it, then get an empty subarray to make the sum equals to 0.

Constraints

  • 1 <= arr.length <= 105
  • -104 <= arr[i] <= 104

Solution

Method 1 – Dynamic Programming (Two States)

Intuition

To find the maximum subarray sum with at most one deletion, we use dynamic programming to track two states: the maximum sum ending at each position without deletion, and the maximum sum ending at each position with one deletion. This allows us to consider both keeping and deleting an element for the best result.

Approach

  1. Use two variables:
    • keep: maximum sum ending at current index without deletion.
    • del: maximum sum ending at current index with one deletion.
  2. For each element:
    • Update keep as the max of current element or previous keep plus current element.
    • Update del as the max of previous keep (delete current) or previous del plus current element.
  3. Track the global maximum among all states.
  4. Return the maximum found.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    int maximumSum(vector<int>& arr) {
        int ans = arr[0], keep = arr[0], del = INT_MIN;
        for (int i = 1; i < arr.size(); ++i) {
            del = max(keep, del + arr[i]);
            keep = max(arr[i], keep + arr[i]);
            ans = max(ans, max(keep, del));
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
func maximumSum(arr []int) int {
    ans, keep, del := arr[0], arr[0], -1<<31
    for i := 1; i < len(arr); i++ {
        del = max(keep, del+arr[i])
        keep = max(arr[i], keep+arr[i])
        ans = max(ans, max(keep, del))
    }
    return ans
}
func max(a, b int) int { if a > b { return a }; return b }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    public int maximumSum(int[] arr) {
        int ans = arr[0], keep = arr[0], del = Integer.MIN_VALUE;
        for (int i = 1; i < arr.length; i++) {
            del = Math.max(keep, del + arr[i]);
            keep = Math.max(arr[i], keep + arr[i]);
            ans = Math.max(ans, Math.max(keep, del));
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    fun maximumSum(arr: IntArray): Int {
        var ans = arr[0]
        var keep = arr[0]
        var del = Int.MIN_VALUE
        for (i in 1 until arr.size) {
            del = maxOf(keep, del + arr[i])
            keep = maxOf(arr[i], keep + arr[i])
            ans = maxOf(ans, keep, del)
        }
        return ans
    }
}
1
2
3
4
5
6
7
8
def maximumSum(arr: list[int]) -> int:
    ans = keep = arr[0]
    del_ = float('-inf')
    for i in range(1, len(arr)):
        del_ = max(keep, del_ + arr[i])
        keep = max(arr[i], keep + arr[i])
        ans = max(ans, keep, del_)
    return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
impl Solution {
    pub fn maximum_sum(arr: Vec<i32>) -> i32 {
        let mut ans = arr[0];
        let mut keep = arr[0];
        let mut del = i32::MIN;
        for i in 1..arr.len() {
            del = keep.max(del + arr[i]);
            keep = arr[i].max(keep + arr[i]);
            ans = ans.max(keep).max(del);
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    maximumSum(arr: number[]): number {
        let ans = arr[0], keep = arr[0], del = -Infinity;
        for (let i = 1; i < arr.length; i++) {
            del = Math.max(keep, del + arr[i]);
            keep = Math.max(arr[i], keep + arr[i]);
            ans = Math.max(ans, keep, del);
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), since we process each element once.
  • 🧺 Space complexity: O(1), only a constant number of variables are used.