Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
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:
Input: arr = [1,-2,-2,3] Output: 3 Explanation: We just choose [3] and it's the maximum sum.
Example 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] <= 104Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,-2,0,3]
[1,-2,-2,3]
[-1,-1,-1,-1]
maximize-subarray-sum-after-removing-all-occurrences-of-one-element)maximum-unique-subarray-sum-after-deletion)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1186: Maximum Subarray Sum with One Deletion
class Solution {
public int maximumSum(int[] arr) {
int n = arr.length;
int[] left = new int[n];
int[] right = new int[n];
int ans = -(1 << 30);
for (int i = 0, s = 0; i < n; ++i) {
s = Math.max(s, 0) + arr[i];
left[i] = s;
ans = Math.max(ans, left[i]);
}
for (int i = n - 1, s = 0; i >= 0; --i) {
s = Math.max(s, 0) + arr[i];
right[i] = s;
}
for (int i = 1; i < n - 1; ++i) {
ans = Math.max(ans, left[i - 1] + right[i + 1]);
}
return ans;
}
}
// Accepted solution for LeetCode #1186: Maximum Subarray Sum with One Deletion
func maximumSum(arr []int) int {
n := len(arr)
left := make([]int, n)
right := make([]int, n)
for i, s := 0, 0; i < n; i++ {
s = max(s, 0) + arr[i]
left[i] = s
}
for i, s := n-1, 0; i >= 0; i-- {
s = max(s, 0) + arr[i]
right[i] = s
}
ans := slices.Max(left)
for i := 1; i < n-1; i++ {
ans = max(ans, left[i-1]+right[i+1])
}
return ans
}
# Accepted solution for LeetCode #1186: Maximum Subarray Sum with One Deletion
class Solution:
def maximumSum(self, arr: List[int]) -> int:
n = len(arr)
left = [0] * n
right = [0] * n
s = 0
for i, x in enumerate(arr):
s = max(s, 0) + x
left[i] = s
s = 0
for i in range(n - 1, -1, -1):
s = max(s, 0) + arr[i]
right[i] = s
ans = max(left)
for i in range(1, n - 1):
ans = max(ans, left[i - 1] + right[i + 1])
return ans
// Accepted solution for LeetCode #1186: Maximum Subarray Sum with One Deletion
impl Solution {
pub fn maximum_sum(arr: Vec<i32>) -> i32 {
let n = arr.len();
let mut left = vec![0; n];
let mut right = vec![0; n];
let mut s = 0;
for i in 0..n {
s = (s.max(0)) + arr[i];
left[i] = s;
}
s = 0;
for i in (0..n).rev() {
s = (s.max(0)) + arr[i];
right[i] = s;
}
let mut ans = *left.iter().max().unwrap();
for i in 1..n - 1 {
ans = ans.max(left[i - 1] + right[i + 1]);
}
ans
}
}
// Accepted solution for LeetCode #1186: Maximum Subarray Sum with One Deletion
function maximumSum(arr: number[]): number {
const n = arr.length;
const left: number[] = Array(n).fill(0);
const right: number[] = Array(n).fill(0);
for (let i = 0, s = 0; i < n; ++i) {
s = Math.max(s, 0) + arr[i];
left[i] = s;
}
for (let i = n - 1, s = 0; i >= 0; --i) {
s = Math.max(s, 0) + arr[i];
right[i] = s;
}
let ans = Math.max(...left);
for (let i = 1; i < n - 1; ++i) {
ans = Math.max(ans, left[i - 1] + right[i + 1]);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.