Given an array of integers, find the longest subarray that contains at most K distinct integers and return its length.
Example
Input: nums =[1, 2, 1, 2, 3]
, k =2
Output: 4 Explanation:the subarray nums[0...3] contains 2 distinct values: [1, 2] and is the longest subarray
Your algorithm should run in O(n^2) time and use O(n) extra space.
The core challenge of this problem is to find the longest contiguous subarray that contains at most K distinct integers. This problem is significant in scenarios where we need to analyze data streams or sequences to find patterns or segments that meet specific criteria. A common pitfall is to misinterpret the requirement of "at most K distinct integers" and mistakenly consider subarrays with exactly K distinct integers.
To solve this problem, we can use a sliding window approach. The idea is to maintain a window that satisfies the condition of having at most K distinct integers and expand or contract this window as we iterate through the array.
A naive solution would involve checking all possible subarrays and counting the distinct integers in each. This approach is not optimal as it has a time complexity of O(n^3) due to the nested loops and the distinct count operation.
An optimized approach uses a sliding window technique with a hash map to keep track of the count of each integer within the window. This allows us to efficiently expand and contract the window while maintaining the count of distinct integers.
Here is a step-by-step breakdown of the sliding window algorithm:
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int longestSubarrayWithKDistinct(vector<int>& nums, int k) {
unordered_map<int, int> countMap; // Hash map to store the count of elements
int left = 0, right = 0;
int maxLength = 0;
while (right < nums.size()) {
// Expand the window by including nums[right]
countMap[nums[right]]++;
// If the window has more than k distinct integers, contract it from the left
while (countMap.size() > k) {
countMap[nums[left]]--;
if (countMap[nums[left]] == 0) {
countMap.erase(nums[left]);
}
left++;
}
// Update the maximum length of the window
maxLength = max(maxLength, right - left + 1);
right++;
}
return maxLength;
}
int main() {
vector<int> nums = {1, 2, 1, 2, 3};
int k = 2;
cout << "Longest subarray length: " << longestSubarrayWithKDistinct(nums, k) << endl;
return 0;
}
The time complexity of the sliding window approach is O(n) because each element is processed at most twice (once when expanding the window and once when contracting it). The space complexity is O(n) due to the hash map used to store the count of elements.
Consider the following edge cases:
To test the solution comprehensively, consider the following test cases:
vector<int> nums1 = {}; int k1 = 2; assert(longestSubarrayWithKDistinct(nums1, k1) == 0); vector<int> nums2 = {1, 1, 1, 1}; int k2 = 1; assert(longestSubarrayWithKDistinct(nums2, k2) == 4); vector<int> nums3 = {1, 2, 3, 4, 5}; int k3 = 2; assert(longestSubarrayWithKDistinct(nums3, k3) == 2); vector<int> nums4 = {1, 2, 1, 3, 4}; int k4 = 3; assert(longestSubarrayWithKDistinct(nums4, k4) == 4);
When approaching such problems, consider the following tips:
In this blog post, we discussed how to solve the problem of finding the longest subarray with at most K distinct integers using a sliding window approach. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills in competitive programming and technical interviews.
For further reading and practice, consider the following resources: