As reported: NetApp Full Stack Engineer, round 1 (2026): "Maximum Sum of Unique Subarray".
Given an integer array nums and an integer k, find the highest sum of a subarray (contiguous) that has exactly k elements, all of them distinct. Return 0 if no such subarray exists.
This is LeetCode 2461 — Maximum Sum of Distinct Subarrays With Length K, with negative values allowed.
Example 1
nums = [3,2,2,3,4,6,7,7,-1], k = 4 -> 20 [3,4,6,7]
Example 2
nums = [5,5,5,5,5], k = 3 -> 0 no window of 3 distinct values
Example 3
nums = [3,2,1,2,4,6,7,7,-1], k = 4 -> 19 [2,4,6,7]
Check every length-4 window: [1,2,4,6] sums to 13 and [2,4,6,7] to 19; the others repeat a value.
Clarifications
- Values can be negative. When valid windows exist, return the largest of their sums even if it is negative; return
0only when there is no valid window.
Constraints: 1 ≤ k ≤ nums.length ≤ 10^5; -10^5 ≤ nums[i] ≤ 10^5.
Code runner format: two lines: nums as a JSON array, then k. Implement maximumSubarraySum(nums, k); the harness prints the returned number.