Maximize Subarray Sum After Removing All Occurrences of One Element

You are given an integer array nums.

You can do the following operation on the array at most once:

  • Choose any integer x such that nums remains non-empty on removing all occurrences of x.
  • Remove all occurrences of x from the array.

Return the maximum subarray sum across all possible resulting arrays.

Example 1
Inputnums = [-3,2,-2,-1,3,-2,3]
Output7
Deleting all occurrences of -2 gives [-3, 2, -1, 3, 3], whose maximum subarray sum is 7, which is best among all possible resulting arrays.
Example 2
Inputnums = [1,2,3,4]
Output10
It is optimal to not perform any operations, so the maximum subarray sum is 1 + 2 + 3 + 4 = 10.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^6 <= nums[i] <= 10^6

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate