Maximum Subarray Sum with One Deletion

MediumDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array arr, choose a non-empty contiguous subarray and then optionally delete at most one element from it. The subarray must still contain at least one element after the deletion. Return the largest possible sum of the remaining elements.

Examples

Input: arr = [1,-2,0,3]
Output: 4
Take `[1,-2,0,3]` and delete `-2`, leaving a sum of 4.
Input: arr = [1,-2,-2,3]
Output: 3
Take `[3]` on its own; bridging to the left would cost at least one `-2`.
Input: arr = [-1,-1,-1,-1]
Output: -1
Every choice is negative; a single `-1` is best (deleting it would leave nothing).

Constraints

  • 1 <= arr.length <= 10^5
  • -10^4 <= arr[i] <= 10^4

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems