Find the maximum possible sum of a subarray with all unique values after deleting at most one element from the array.
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.
nums.nums[i] is an integer value.1 <= nums.lengthExample 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
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.