Find the maximum possible sum of a subarray with all unique elements.
Maximum Erasure Value
Given an integer array nums, choose a contiguous subarray that contains no repeated values. Your task is to return the largest possible sum of such a subarray.
A subarray must consist of consecutive elements from the original array. If multiple unique subarrays exist, you only need the maximum sum.
Goal
Compute the maximum sum over all subarrays whose elements are pairwise distinct.
Input Format
- An integer array
nums. nums[i]is an integer value in the array.
Output Format
- Return an integer representing the maximum sum of any contiguous subarray with all distinct elements.
Constraints
- The subarray must be contiguous.
- All elements in the chosen subarray must be unique.
- Return the maximum sum among all valid subarrays.
Example 1
Input
nums = [4,2,4,5,6]
Output
17
Explanation
The subarray [2,4,5,6] has all unique elements and sum 17, which is the maximum.
Example 2
Input
nums = [5,2,1,2,5,2,1,2,5]
Output
8
Explanation
The best unique subarray is [5,2,1] or [1,2,5], both with sum 8.
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.