Daily DSA: Longest Subarray with At Most K Distinct Even Numbers (Medium)
Advertisement
Problem Description
Given an array of positive integers nums and an integer k, return the maximum length of a contiguous subarray that contains at most k distinct even numbers.
An even number is an integer that is divisible by 2. Odd numbers do not count towards the distinct even numbers limit.
Examples
Example 1:
Input: nums = [1, 2, 4, 2, 3, 6, 8], k = 2
Output: 5
Explanation: The longest valid subarray is [1, 2, 4, 2, 3].
The distinct even numbers in this subarray are {2, 4}, which is 2 distinct even numbers (<= k).
The length of this subarray is 5.
Example 2:
Input: nums = [2, 4, 6, 8], k = 1
Output: 1
Explanation: Any subarray of length 2 or more contains at least 2 distinct even numbers. Thus, the maximum length is 1 (e.g., [2], [4], [6], or [8]).
Example 3:
Input: nums = [1, 3, 5, 7, 2, 9, 11], k = 0
Output: 3
Explanation: With k = 0, no even numbers are allowed. The subarray [1, 3, 5] (or [3, 5, 7]) contains 0 distinct even numbers and has length 3.
Constraints
1 <= nums.length <= 10^51 <= nums[i] <= 10^90 <= k <= nums.length
Approach & Explanation
This problem can be efficiently solved using the Sliding Window (Two Pointers) technique:
- Window Definition: Maintain a window
[left, right]and a hash mapeven_freqto keep track of the frequencies of even numbers within the current window. - Expand Window: Iterate
rightfrom0tonums.length - 1. Ifnums[right]is even, increment its frequency count ineven_freq. - Shrink Window: If the number of distinct even elements (i.e.,
even_freq.size()) exceedsk, shrink the window from the left by incrementingleftuntileven_freq.size() <= k. Ifnums[left]is even, decrement its frequency ineven_freqand remove the key if its count reaches0. - Update Result: At each step where the window is valid (
even_freq.size() <= k), update the maximum length asmax_len = max(max_len, right - left + 1).
C++ Source Code
#include <vector>
#include <unordered_map>
#include <algorithm>
class Solution {
public:
int maxSubarrayLength(std::vector<int>& nums, int k) {
std::unordered_map<int, int> even_freq;
int left = 0;
int max_len = 0;
for (int right = 0; right < nums.size(); ++right) {
// If the element is even, track its frequency
if (nums[right] % 2 == 0) {
even_freq[nums[right]]++;
}
// Shrink window if distinct even count exceeds k
while (even_freq.size() > k) {
if (nums[left] % 2 == 0) {
even_freq[nums[left]]--;
if (even_freq[nums[left]] == 0) {
even_freq.erase(nums[left]);
}
}
left++;
}
// Update maximum length
max_len = std::max(max_len, right - left + 1);
}
return max_len;
}
};
Complexity Analysis
- Time Complexity: $\mathcal{O}(N)$, where $N$ is the length of
nums. Each element is processed at most twice (once when expanded byright, once when shrunk byleft). Hash map lookup and insertion are $\mathcal{O}(1)$ on average. - Space Complexity: $\mathcal{O}(N)$ in the worst case (or $\mathcal{O}(K)$) to store frequencies of distinct even integers in the hash map.
Advertisement