Talentd/FleetCode/Problems/

4072. Maximum Alternating Subarray Sum With One Deletion

Medium
Read SolutionWatch Video

4072. Maximum Alternating Subarray Sum With One Deletion

Medium29.6% Acceptance

You are given an integer array nums.

You may delete at most one element from nums, then choose a subarray of the resulting array.

Return the maximum possible alternating sum of the chosen subarray.

The alternating sum of an array is the sum of its elements at even indices minus the sum of its elements at odd indices. The chosen subarray is reindexed starting from 0 before calculating its alternating sum.

Example 1:

Input: nums = [5,-5,1]

Output: 11

Explanation:

Choose not to delete an element and select the entire array. Its alternating sum is 5 - (-5) + 1 = 11, which is the maximum possible.

Example 2:

Input: nums = [10,-5,-100]

Output: 110

Explanation:

Delete nums[1] = -5 to obtain [10,-100], then select the entire resulting array. Its alternating sum is 10 - (-100) = 110, which is the maximum possible.

Example 3:

Input: nums = [4,7]

Output: 7

Explanation:

Choose not to delete an element and select the subarray [7]. Its alternating sum is 7, which is the maximum possible.

Constraints:

  • 1 <= nums.length <= 105
  • -105 <= nums[i] <= 105
Similar Questions
Loading editor...
[5,-5,1]