Maximize Subarray Sum After Removing All Occurrences of One Element - Problem

You're given an integer array nums, and you have the power to strategically eliminate all occurrences of any single value from the array. Your mission? Maximize the resulting subarray sum!

Here's how it works:

  • Choose any integer x that appears in the array
  • Remove all occurrences of x from nums
  • The array must remain non-empty after removal
  • Find the maximum possible subarray sum from the resulting array

Goal: Return the maximum subarray sum you can achieve across all possible ways of removing elements.

Example: For nums = [1, -2, 0, 3], you could remove all -2s to get [1, 0, 3] with max subarray sum of 4, or remove all 0s to get [1, -2, 3] with max subarray sum of 3.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, -2, 0, 3]
โ€บ Output: 4
๐Ÿ’ก Note: Remove all occurrences of -2 to get [1, 0, 3]. The maximum subarray sum is 1 + 0 + 3 = 4.
example_2.py โ€” All Negative
$ Input: [-1, -2, -3]
โ€บ Output: -1
๐Ÿ’ก Note: Remove -3 and -2 to get [-1] or remove -1 and -3 to get [-2]. Best option is removing -2 and -3 to get [-1], giving maximum subarray sum of -1.
example_3.py โ€” Mixed Values
$ Input: [2, -1, 2, 1, -2, 1]
โ€บ Output: 6
๐Ÿ’ก Note: Remove all occurrences of -1 and -2. If we remove -2, we get [2, -1, 2, 1, 1] with max subarray sum of 5. If we remove -1, we get [2, 2, 1, -2, 1] with max subarray sum of 5. But removing -2 gives us [2, -1, 2, 1, 1] and the subarray [2, 1, 1] = 4 or [2] + [2, 1, 1] through kadane gives us 6.

Visualization

Tap to expand
Maximize Subarray Sum StrategyOriginal Array: [1, -2, 0, 3]1-203Strategy 1: Remove -2103Max Subarray: 1+0+3 = 4Strategy 2: Remove 01-23Max Subarray: [3] = 3Kadane's Algorithm in Action:For array [1, 0, 3]:โ€ข i=0: max_here=1, max_so_far=1โ€ข i=1: max_here=max(0,1+0)=1, max_so_far=1โ€ข i=2: max_here=max(3,1+3)=4, max_so_far=4โ€ข Result: 4๐Ÿ† Optimal Result: 4
Understanding the Visualization
1
Catalog Unique Types
Identify all unique values (photo types) in the array
2
Simulate Each Removal
For each unique value, simulate removing all its occurrences
3
Apply Kadane's Magic
Use Kadane's algorithm to efficiently find maximum subarray sum
4
Track Global Maximum
Keep track of the best result across all removal strategies
Key Takeaway
๐ŸŽฏ Key Insight: We only need to try removing each unique value once, then use Kadane's algorithm to efficiently find the maximum subarray sum in the remaining elements. This gives us O(kร—n) complexity where k is the number of unique values.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(k ร— n)

k unique values ร— n elements for Kadane's algorithm on each filtered array

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for storing the filtered array after removing elements

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • -104 โ‰ค nums[i] โ‰ค 104
  • The array must remain non-empty after removal
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
34.3K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen