C++ | Map | Sliding Window | Two Pointers | With Comments
\nclass Solution {\npublic:\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        int n = (int)nums.size(), ans1 = 0, l = 0, ans2 = 0;\n        m
```\nclass Solution {\npublic:\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        int n = (int)nums.size(), ans1 = 0, l = 0, ans2 = 0;\n        map<int, int> mp;\n        //Calculte Subarrays with distinct integers < k\n        for(int r = 0; r < n; r++){\n            mp[nums[r]]++;\n            while(mp.size() >= k){\n                if(--mp[nums[l]] == 0) mp.erase(nums[l]);\n                l++;\n            }\n            ans1 += (r - l + 1);\n        }\n        l = 0, mp.clear();\n        //Calculate Subarrays with distinct integers <= k\n        for(int r = 0; r < n; r++){\n            mp[nums[r]]++;\n            while(mp.size() > k){\n                if(--mp[nums[l]] == 0) mp.erase(nums[l]);\n                l++;\n            }\n            ans2 += (r - l + 1);\n        }\n        // final ans  =  subarrays with <=k distinct integers - subarrays with < k distinct integers\n        return ans2 - ans1;\n    }\n};\n```
['Two Pointers', 'C', 'Sliding Window']