Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Amazon
Maximum Erasure Value

Find the maximum possible sum of a subarray with all unique elements.

Acceptance 0%
Problem Statement

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.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.