Find the maximum possible sum of a subarray with all unique values after deleting at most one element from the array.
Problem
You are given an integer array nums. You may delete at most one element from the array.
After the deletion, choose a contiguous subarray of the remaining array such that all values in that subarray are distinct. Your goal is to maximize the sum of the chosen subarray.
Return the maximum sum you can obtain.
A subarray is a contiguous portion of the array.
Notes
- You may choose not to delete any element.
- The chosen subarray must contain pairwise distinct values.
- The answer should be the best sum over all valid choices of deletion and subarray selection.
Input Format
- The input is an integer array
nums. nums[i]is an integer value.
Output Format
- Return a single integer: the maximum sum of a valid subarray after deleting at most one element.
Constraints
1 <= nums.length- Values and exact bounds are not guaranteed from the source; assume standard interview-style integer array constraints.
- The subarray must contain only distinct values.
- You may delete at most one element total before selecting the subarray.
Example 1
Input
nums = [1,2,3,2,4]
Output
10
Explanation
Delete the second 2, then take the subarray [1,2,3,4], which has all unique values and sum 10.
Example 2
Input
nums = [5,2,1,2,5,2,1,2,5]
Output
8
Explanation
One optimal choice is to delete a middle element to obtain a unique subarray such as [5,2,1] with sum 8. No larger unique-sum choice is possible.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.