Last Updated: March 21, 2026
We need to find the maximum sum among all subarrays of exactly length k where every element in the subarray is unique. If no such subarray exists, return 0.
Two things matter here: the subarray must be exactly k elements long, and it must contain no duplicates. The sum part is easy once you've identified valid subarrays. The real question is how to efficiently check for duplicates as you slide through the array.
Notice that the constraint 1 <= nums[i] <= 10^5 means all values are positive. This is important because it means adding more elements always increases the sum. So among valid windows, we just want the one with the largest total.
nums.length up to 10^5 means we need O(n log n) or better. An O(n * k) brute force could work when k is small, but degrades to O(n^2) when k approaches n.k <= nums.length so we're guaranteed at least one subarray of length k exists (though it might not be valid).[1, 10^5], all positive. No negative numbers or zeros to worry about.The most straightforward approach: examine every subarray of length k, check if all its elements are distinct, and if so, track the maximum sum. We can use a hash set to check for duplicates within each subarray.
This is the literal translation of the problem statement into code. For each starting index, grab k elements, verify uniqueness, compute the sum.
maxSum = 0i from 0 to n - k:nums[i] through nums[i + k - 1] to the set while accumulating the sumk elements (all distinct) and the sum exceeds maxSum, update maxSummaxSumn - k + 1 starting positions, we examine up to k elements. In the worst case where there are no duplicates, every subarray is fully scanned.k elements.For each new subarray, we're rebuilding the set and recomputing the sum from scratch. But adjacent subarrays overlap in k - 1 elements. What if we kept the set and sum from the previous window and just updated the one element that changed?
When we slide the window one position to the right, exactly one element leaves (the leftmost) and one element enters (the new rightmost). Instead of rebuilding everything, we can maintain a running sum and a frequency map, updating both incrementally.
The frequency map tracks how many times each value appears in the current window. A window is valid (all distinct) when every value has a count of exactly 1, which happens when the number of distinct keys in the map equals k. So we maintain a count of distinct elements and compare it to k to check validity.
The frequency map's size tells us exactly how many distinct values are in the current window. If the window has k elements and the map also has k keys, every element must appear exactly once, meaning all elements are distinct. By cleaning up zero-count entries immediately, the map size always reflects the true number of distinct values.
The running sum avoids recalculating from scratch each time. Since we add the incoming element's value and subtract the outgoing element's value, the sum stays accurate through each slide.
windowSum = 0, maxSum = 0nums[0] through nums[k-1] to the map and sumk distinct elements (map size equals k), update maxSumk to n - 1:nums[i] to the map and add its value to windowSumnums[i - k] from the map and subtract its value from windowSumk (all distinct), update maxSummaxSumk entries (one for each element in the current window).