2461. Maximum Sum of Distinct Subarrays With Length K LeetCode Solution
In this guide, you will get 2461. Maximum Sum of Distinct Subarrays With Length K LeetCode Solution with the best time and space complexity. The solution to Maximum Sum of Distinct Subarrays With Length K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Sum of Distinct Subarrays With Length K solution in C++
- Maximum Sum of Distinct Subarrays With Length K solution in Java
- Maximum Sum of Distinct Subarrays With Length K solution in Python
- Additional Resources

Problem Statement of Maximum Sum of Distinct Subarrays With Length K
You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions:
The length of the subarray is k, and
All the elements of the subarray are distinct.
Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return 0.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,5,4,2,9,9,9], k = 3
Output: 15
Explanation: The subarrays of nums with length 3 are:
– [1,5,4] which meets the requirements and has a sum of 10.
– [5,4,2] which meets the requirements and has a sum of 11.
– [4,2,9] which meets the requirements and has a sum of 15.
– [2,9,9] which does not meet the requirements because the element 9 is repeated.
– [9,9,9] which does not meet the requirements because the element 9 is repeated.
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
Example 2:
Input: nums = [4,4,4], k = 3
Output: 0
Explanation: The subarrays of nums with length 3 are:
– [4,4,4] which does not meet the requirements because the element 4 is repeated.
We return 0 because no subarrays meet the conditions.
Constraints:
1 <= k <= nums.length <= 105
1 <= nums[i] <= 105
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2461. Maximum Sum of Distinct Subarrays With Length K LeetCode Solution in C++
class Solution {
public:
long long maximumSubarraySum(vector<int>& nums, int k) {
long ans = 0;
long sum = 0;
int distinct = 0;
unordered_map<int, int> count;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
if (++count[nums[i]] == 1)
++distinct;
if (i >= k) {
if (--count[nums[i - k]] == 0)
--distinct;
sum -= nums[i - k];
}
if (i >= k - 1 && distinct == k)
ans = max(ans, sum);
}
return ans;
}
};
/* code provided by PROGIEZ */
2461. Maximum Sum of Distinct Subarrays With Length K LeetCode Solution in Java
class Solution {
public long maximumSubarraySum(int[] nums, int k) {
long ans = 0;
long sum = 0;
int distinct = 0;
Map<Integer, Integer> count = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
sum += nums[i];
if (count.merge(nums[i], 1, Integer::sum) == 1)
++distinct;
if (i >= k) {
if (count.merge(nums[i - k], -1, Integer::sum) == 0)
--distinct;
sum -= nums[i - k];
}
if (i >= k - 1 && distinct == k)
ans = Math.max(ans, sum);
}
return ans;
}
}
// code provided by PROGIEZ
2461. Maximum Sum of Distinct Subarrays With Length K LeetCode Solution in Python
class Solution:
def maximumSubarraySum(self, nums: list[int], k: int) -> int:
ans = 0
summ = 0
distinct = 0
count = collections.Counter()
for i, num in enumerate(nums):
summ += num
count[num] += 1
if count[num] == 1:
distinct += 1
if i >= k:
count[nums[i - k]] -= 1
if count[nums[i - k]] == 0:
distinct -= 1
summ -= nums[i - k]
if i >= k - 1 and distinct == k:
ans = max(ans, summ)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.