Problem · Array

Maximum Sum of a Unique-Element Subarray

Learn this problem
MediumTekion logoTekionFULLTIMEONSITE INTERVIEW

Problem statement

Given a non-empty integer array nums containing only non-negative values, return the maximum sum of a contiguous subarray whose elements are all distinct.

Function

maximumUniqueSubarraySum(nums: int[]) → int

Examples

Example 1

nums = [4,2,4,5,6]return = 17

The subarray [2,4,5,6] has distinct values and sum 17, which is maximal.

Example 2

nums = [5,2,1,2,5,2,1,2,5]return = 8

A maximum-sum distinct subarray is [5,2,1], whose sum is 8.

Example 3

nums = [0,0,0]return = 0

Any one-element subarray [0] is valid and has sum 0.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^4
  • The answer fits in a 32-bit signed integer.

More Tekion problems

drafts saved locally
public int maximumUniqueSubarraySum(int[] nums) {
    // write your code here
}
nums[4,2,4,5,6]
expected17
checking account