Maximize Subarray Sum After Removing All Occurrences of One Element - Solution & Explanation
Problem Statement
You are given an integer array nums.
You can do the following operation on the array at most once:
- Choose any integer
xsuch thatnumsremains non-empty on removing all occurrences ofx. - Remove all occurrences of
xfrom the array.
Return the maximum subarray sum across all possible resulting arrays.
Example 1:
Input: nums = [-3,2,-2,-1,3,-2,3]
Output: 7
Explanation:
We can have the following arrays after at most one operation:
- The original array is
nums = [-3, 2, -2, -1, 3, -2, 3]. The maximum subarray sum is3 + (-2) + 3 = 4. - Deleting all occurences of
x = -3results innums = [2, -2, -1, 3, -2, 3]. The maximum subarray sum is3 + (-2) + 3 = 4. - Deleting all occurences of
x = -2results innums = [-3, 2, -1, 3, 3]. The maximum subarray sum is2 + (-1) + 3 + 3 = 7. - Deleting all occurences of
x = -1results innums = [-3, 2, -2, 3, -2, 3]. The maximum subarray sum is3 + (-2) + 3 = 4. - Deleting all occurences of
x = 3results innums = [-3, 2, -2, -1, -2]. The maximum subarray sum is 2.
The output is max(4, 4, 7, 4, 2) = 7.
Example 2:
Input: nums = [1,2,3,4]
Output: 10
Explanation:
It is optimal to not perform any operations.
Constraints:
1 <= nums.length <= 105-106 <= nums[i] <= 106
Approach Overview
Problem Overview: You get an integer array nums. Pick exactly one value x and remove all its occurrences from the array. After the removal, the array closes the gaps. Your goal is to compute the maximum possible subarray sum in the resulting array.
Approach 1: Try Removing Each Value + Kadane (O(n * u) time, O(1) space)
The most direct approach is to simulate the operation for every distinct value in the array. For a candidate value x, iterate through the array and skip all elements equal to x. While scanning the remaining elements, run Kadane’s algorithm to compute the maximum subarray sum. Repeat this process for every unique value u. This approach clearly demonstrates the problem mechanics but becomes slow when the array contains many distinct values because the entire array is scanned repeatedly.
Approach 2: Segment Tree with Temporary Updates (O(n log n) time, O(n) space)
A more scalable strategy uses a segment tree that stores the standard maximum subarray metrics: sum, maxPrefix, maxSuffix, and maxSubarray. First build the tree over the original array. Then group indices by value. For a value x, temporarily update all its positions in the tree to 0, which simulates removing those elements while allowing adjacent segments to merge. After applying these updates, the root node immediately gives the maximum subarray sum of the modified array. Record the result, then revert the updates before testing the next value.
This works because replacing removed elements with 0 preserves continuity of the subarray while eliminating their contribution. The segment tree recomputes prefix/suffix merges in O(log n) per update, making the total cost proportional to the number of elements processed.
Recommended for interviews: Interviewers typically expect a solution that avoids recomputing Kadane for every value. Explaining the brute force approach shows you understand the effect of removing a value. Implementing the optimized version with a segment tree and concepts from dynamic programming demonstrates stronger algorithmic thinking and reduces the complexity to O(n log n).
Solutions for this problem are being prepared.
Try solving it yourselfDetailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Try Removing Each Value + Kadane | O(n * u) | O(1) | Useful for understanding the problem or when the number of distinct values is very small |
| Segment Tree with Temporary Updates | O(n log n) | O(n) | Best general solution for large arrays with many distinct values |
Video Solution
3410. Maximize Subarray Sum After Removing All Occurrences of One Element (Leetcode Hard) • Programming Live with Larry • 1,062 views views
Watch 1 more video solutions →Frequently Asked Questions
Is Maximize Subarray Sum After Removing All Occurrences of One Element easy or hard?
Maximize Subarray Sum After Removing All Occurrences of One Element Python/Java solution
How to solve Maximize Subarray Sum After Removing All Occurrences of One Element in O(n)?
What is the best approach for Maximize Subarray Sum After Removing All Occurrences of One Element?
Is Maximize Subarray Sum After Removing All Occurrences of One Element asked at Google/Amazon/Meta?
What data structure is used in Maximize Subarray Sum After Removing All Occurrences of One Element?
What is the time complexity of Maximize Subarray Sum After Removing All Occurrences of One Element?
Ready to solve this problem?
Practice Maximize Subarray Sum After Removing All Occurrences of One Element with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor