Best Nonempty Subarray After At Most One Erasure

Quick Overview

Best Nonempty Subarray After At Most One Erasure. Choose a contiguous subarray of arr and optionally erase one of its elements.

Best Nonempty Subarray After At Most One Erasure

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Choose a contiguous subarray of arr and optionally erase one of its elements. At least one chosen element must remain. Return the greatest sum attainable; deletion is optional. ```cpp class Solution { public: int maximumSum(vector<int>& arr); }; ``` ### Examples **Example 1:** ```text Input: arr = [4,-5,3,2] Output: 9 Explanation: Choose the full array and erase-5, retaining4+3+2. ``` **Example 2:** ```text Input: arr = [-4,-2,-7] Output: -2 Explanation: Keep the singleton-2. An empty result is forbidden. ``` ### Constraints - There are 1 to 100000 elements. - Each value is an integer in `[-10000,10000]`. ### Contract considerations The original interval is contiguous; after the optional deletion, its remaining elements need not be adjacent in the original array. ```hint Read the boundary condition If all entries are negative, may the chosen interval become empty after the optional erasure? ``` ### Boundary reminders The original interval is contiguous; after the optional deletion, its remaining elements need not be adjacent in the original array.

Overview: Best Nonempty Subarray After At Most One Erasure. Choose a contiguous subarray of arr and optionally erase one of its elements.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Choose a contiguous subarray of arr and optionally erase one of its elements. At least one chosen element must remain. Return the greatest sum attainable; deletion is optional.

class Solution {
public:
    int maximumSum(vector<int>& arr);
};

Examples

Example 1:

Input: arr = [4,-5,3,2]
Output: 9
Explanation: Choose the full array and erase-5, retaining4+3+2.

Example 2:

Input: arr = [-4,-2,-7]
Output: -2
Explanation: Keep the singleton-2. An empty result is forbidden.

Constraints

  • There are 1 to 100000 elements.
  • Each value is an integer in [-10000,10000] .

Contract considerations

The original interval is contiguous; after the optional deletion, its remaining elements need not be adjacent in the original array.

Boundary reminders

The original interval is contiguous; after the optional deletion, its remaining elements need not be adjacent in the original array.