2461. Maximum Sum of Distinct Subarrays With Length K
Asked at Walmart, JPMorgan
Difficulty
Medium
Time
O(n)
Space
O(k)
Pattern
Array
Companies
2
Problem
You are given an integer array nums and an integer k. Look at every subarray of nums that has exactly length k. A subarray means a block of neighbours — the elements must be next to each other in the array.
Among those length-k blocks, keep only the ones where all elements are different (no value appears twice inside the block). From the blocks that pass this test, return the largest sum. If no block passes the test, return 0.
Note that a block can fail only because of a repeated value. Its position in the array does not matter, and you never need to sort or reorder anything.
Only clean blocks of length k count
Constraints
- ●
1 <= k <= nums.length <= 10^5 - ●
1 <= nums[i] <= 10^5
Examples
nums = [1,5,4,2,9,9,9], k = 315nums = [4,4,4], k = 30Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Walmart | Medium | View all Walmart questions → |
| JPMorgan | Medium | View all JPMorgan questions → |
How to Think About It
Brute force: check every subarray of length k, verify uniqueness with a set, and track the maximum sum.
Optimize by using a sliding window of fixed size k with a hash map to count element frequencies.
Expand the window by adding elements from the right; when window size exceeds k, remove the leftmost element.
When a duplicate is detected, shrink the window from the left until the duplicate is removed.
Maintain a running sum, add the new element on expansion, subtract the removed element on shrink, and update the max sum.
Optimal Approach
Use a sliding window of fixed size k with a hash map tracking element frequencies. As you expand the right pointer, add elements and their counts. When a duplicate is detected (count > 1), shrink from the left until the duplicate is removed. When the window reaches size k, update the maximum sum. This runs in O(n) time with O(k) space.
Sliding window with a frequency map
What Trips People Up in Real Interviews
Clarify whether elements must be strictly distinct or if the subarray itself must contain no duplicates.
Edge case: all elements in the array are the same — the answer is 0 since no subarray of length k has distinct elements.
Edge case: array length equals k — check if all k elements are distinct and return their sum.
Distinguish this from classic maximum sum subarray problems — the distinct constraint changes the approach entirely.
Watch for integer overflow if element values are large; use long for the running sum in production code.
Solution Code
O(n) timedef maximumSubarraySum(nums, k):
from collections import defaultdict
count = defaultdict(int)
curr_sum = 0
max_sum = 0
left = 0
for right in range(len(nums)):
count[nums[right]] += 1
curr_sum += nums[right]
if right - left + 1 > k:
count[nums[left]] -= 1
curr_sum -= nums[left]
if count[nums[left]] == 0:
del count[nums[left]]
left += 1
if right - left + 1 == k and len(count) == k:
max_sum = max(max_sum, curr_sum)
return max_sumFrequently Asked Questions
What is the Maximum Sum of Distinct Subarrays With Length K problem?
Given an integer array nums and an integer k, find the maximum sum of a subarray of length k where all elements are distinct. If no such subarray exists, return 0. A subarray is a contiguous non-empty sequence of elements within an array.
How do you solve Maximum Sum of Distinct Subarrays With Length K?
The optimal approach runs in O(n) time and O(k) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Maximum Sum of Distinct Subarrays With Length K?
Maximum Sum of Distinct Subarrays With Length K is asked at Walmart, JPMorgan. It is a medium difficulty problem.
What are common mistakes on Maximum Sum of Distinct Subarrays With Length K?
- Clarify whether elements must be strictly distinct or if the subarray itself must contain no duplicates.
- Edge case: all elements in the array are the same — the answer is 0 since no subarray of length k has distinct elements.
- Edge case: array length equals k — check if all k elements are distinct and return their sum.
- Distinguish this from classic maximum sum subarray problems — the distinct constraint changes the approach entirely.
- Watch for integer overflow if element values are large; use long for the running sum in production code.