category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

992. Subarrays with K Different Integers Hard, 2 pointers

atmost(k) - atmost(k - 1) method

Approach

Code ```cpp int atMost(const vector &nums, const int &k) { if (k == 0) return 0; map<int, int> mp; int left = 0, right = 0, ans = 0; for (int right = 0; right < nums.size(); right++) { mp[nums[right]]++; while (left <= right and mp.size() > k) { mp[nums[left]]--; if (mp[nums[left]] == 0) mp.erase(mp.find(nums[left])); left++; } ans += (right - left + 1); } return ans; } int subarraysWithKDistinct(vector &nums, int k) { return atMost(nums, k) - atMost(nums, k - 1); } ``` </details>