The Indian Engineer

Problem 2461 Maximum Sum of Distinct Subarrays With Length K

Posted on 5 mins

Array Hash-Table Sliding Window

Problem Statement

Link - Problem 2461

Question

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:

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:

Solution

class Solution {
public:
    long long maximumSubarraySum(vector<int>& nums, int k) {
        long long ans = 0;
        long long curr_sum = 0;
        unordered_map<int, int> freq;

        for (int i = 0; i < nums.size(); ++i) {
            curr_sum += nums[i];
            freq[nums[i]]++;

            if (i >= k) {
                int to_remove = nums[i - k];
                curr_sum -= to_remove;
                freq[to_remove]--;

                if (freq[to_remove] == 0) {
                    freq.erase(to_remove);
                }
            }

            if (freq.size() == k) {
                ans = max(ans, curr_sum);
            }
        }

        return ans;
    }
};

Complexity Analysis

| Algorithm                   | Time Complexity | Space Complexity |
| --------------------------- | --------------- | ---------------- |
| Sliding Window with Hashing | O(n)            | O(min(n, k))     |

Explanation

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Which elements change when moving from the subarray of size k that ends at index i to the subarray of size k that ends at index i + 1?

Only two elements change, the element at i + 1 is added into the subarray, and the element at i - k + 1 gets removed from the subarray.

Iterate through each subarray of size k and keep track of the sum of the subarray and the frequency of each element.


Similar Questions:

Title URL Difficulty
Max Consecutive Ones III https://leetcode.com/problems/max-consecutive-ones-iii Medium
Longest Nice Subarray https://leetcode.com/problems/longest-nice-subarray Medium
Optimal Partition of String https://leetcode.com/problems/optimal-partition-of-string Medium
Count the Number of Good Subarrays https://leetcode.com/problems/count-the-number-of-good-subarrays Medium
Maximum Good Subarray Sum https://leetcode.com/problems/maximum-good-subarray-sum Medium
Find the Power of K-Size Subarrays I https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i Medium
Find the Power of K-Size Subarrays II https://leetcode.com/problems/find-the-power-of-k-size-subarrays-ii Medium