Maximum Alternating Subarray Sum With One Deletion - Solution & Explanation
Problem Statement
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
Approach Overview
Problem Overview: You pick a subarray and apply alternating signs (+, -, +, ...) to its elements. You may delete one element from the subarray before summing. Return the maximum possible value. Deleting an element shifts the signs of everything after it, so you can't just drop a value and keep the rest of the signs fixed.
Approach 1: Brute Force Over Every Subarray and Deletion (O(n^3) time, O(1) space)
Fix a start index l and an end index r, then try every index to delete, plus the option of deleting nothing. For each candidate, recompute the signed sum from scratch. This is correct because it checks every legal configuration. It is too slow for n up to 10^5, but it gives you a baseline and helps you confirm the sign-flip rule on small inputs.
Approach 2: Fix the Start, Extend with a Running Sum (O(n^2) time, O(1) space)
For each start index, walk right and keep two running values: the alternating sum with no deletion used, and the best sum where one element has already been removed. Each step updates both values in constant time. This removes the innermost loop over deletions. It is a good intermediate answer if you can't see the single-pass DP yet.
Approach 3: Single-Pass DP with Sign States (O(n) time, O(1) space)
Track the best value ending at index i in four states: whether the element at i carries a + or - sign, and whether a deletion has been spent. Each state at i depends only on states at i-1 and i-2, so one left-to-right pass fills them in. The key observation is that deleting element j means the next kept element inherits j's sign. That lets a deletion transition come from two positions back. Keep a running global maximum over all valid end states. Because each index does a constant amount of work, the total is O(n) with O(1) extra space.
Recommended for interviews: Interviewers expect you to start with the brute force, explain why deletions break a naive prefix-sum approach, and then move to the O(n) DP. The brute force shows you understand the problem. The single-pass DP with explicit sign states shows you can design the state machine yourself. Practice the state transitions on paper before you code. Interviewers often ask you to justify each transition.
Related practice: dynamic programming, arrays, and prefix sum.
Solutions for this problem are being prepared.
Try solving it yourselfDetailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force (every subarray and deletion) | O(n^3) | O(1) | Small inputs, verifying results |
| Fix Start with Running Sums | O(n^2) | O(1) | Moderate n, intermediate step to DP |
| Single-Pass DP with Sign States | O(n) | O(1) | General case, large n, interview answer |
Video Solutions (5)
Leetcode 4072 | Maximum Alternating Subarray Sum With One Deletion | Leetcode weekly contest 522
Frequently Asked Questions
Is Maximum Alternating Subarray Sum With One Deletion easy or hard?
How to solve Maximum Alternating Subarray Sum With One Deletion in O(n)?
Maximum Alternating Subarray Sum With One Deletion Python/Java solution?
What is the best approach for Maximum Alternating Subarray Sum With One Deletion?
Is Maximum Alternating Subarray Sum With One Deletion asked at Google/Amazon/Meta?
What data structure is used in Maximum Alternating Subarray Sum With One Deletion?
What is the time complexity of Maximum Alternating Subarray Sum With One Deletion?
Ready to solve this problem?
Practice Maximum Alternating Subarray Sum With One Deletion with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor