Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array nums.
You are allowed to delete any number of elements from nums without making it empty. After performing the deletions, select a subarray of nums such that:
Return the maximum sum of such a subarray.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 15
Explanation:
Select the entire array without deleting any element to obtain the maximum sum.
Example 2:
Input: nums = [1,1,0,1,1]
Output: 1
Explanation:
Delete the element nums[0] == 1, nums[1] == 1, nums[2] == 0, and nums[3] == 1. Select the entire array [1] to obtain the maximum sum.
Example 3:
Input: nums = [1,2,-1,-2,1,0,-1]
Output: 3
Explanation:
Delete the elements nums[2] == -1 and nums[3] == -2, and select the subarray [2, 1] from [1, 2, 1, 0, -1] to obtain the maximum sum.
Constraints:
1 <= nums.length <= 100-100 <= nums[i] <= 100Problem summary: You are given an integer array nums. You are allowed to delete any number of elements from nums without making it empty. After performing the deletions, select a subarray of nums such that: All elements in the subarray are unique. The sum of the elements in the subarray is maximized. Return the maximum sum of such a subarray.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Greedy
[1,2,3,4,5]
[1,1,0,1,1]
[1,2,-1,-2,1,0,-1]
maximum-subarray-sum-with-one-deletion)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3487: Maximum Unique Subarray Sum After Deletion
class Solution {
public int maxSum(int[] nums) {
int mx = Arrays.stream(nums).max().getAsInt();
if (mx <= 0) {
return mx;
}
boolean[] s = new boolean[201];
int ans = 0;
for (int x : nums) {
if (x < 0 || s[x]) {
continue;
}
ans += x;
s[x] = true;
}
return ans;
}
}
// Accepted solution for LeetCode #3487: Maximum Unique Subarray Sum After Deletion
func maxSum(nums []int) (ans int) {
mx := slices.Max(nums)
if mx <= 0 {
return mx
}
s := make(map[int]bool)
for _, x := range nums {
if x < 0 || s[x] {
continue
}
ans += x
s[x] = true
}
return
}
# Accepted solution for LeetCode #3487: Maximum Unique Subarray Sum After Deletion
class Solution:
def maxSum(self, nums: List[int]) -> int:
mx = max(nums)
if mx <= 0:
return mx
ans = 0
s = set()
for x in nums:
if x < 0 or x in s:
continue
ans += x
s.add(x)
return ans
// Accepted solution for LeetCode #3487: Maximum Unique Subarray Sum After Deletion
use std::collections::HashSet;
impl Solution {
pub fn max_sum(nums: Vec<i32>) -> i32 {
let mx = *nums.iter().max().unwrap_or(&0);
if mx <= 0 {
return mx;
}
let mut s = HashSet::new();
let mut ans = 0;
for &x in &nums {
if x < 0 || s.contains(&x) {
continue;
}
ans += x;
s.insert(x);
}
ans
}
}
// Accepted solution for LeetCode #3487: Maximum Unique Subarray Sum After Deletion
function maxSum(nums: number[]): number {
const mx = Math.max(...nums);
if (mx <= 0) {
return mx;
}
const s = new Set<number>();
let ans: number = 0;
for (const x of nums) {
if (x < 0 || s.has(x)) {
continue;
}
ans += x;
s.add(x);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.