Easy Sliding Window With Full Explanation || Time O(N) Space O(N)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nThis code implements a solution to find the number of subarrays in an arr
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis code implements a solution to find the number of subarrays in an array `nums` that contain exactly `k` distinct elements. Here\'s a brief explanation of the algorithm:\n\n1. `checkSubarrayAtmostK`: This function calculates the number of subarrays with at most `k` distinct elements. It uses a sliding window approach with two pointers, `i` and `j`, to maintain a window of elements. It also uses an unordered map `mpp` to keep track of the count of each element within the current window.\n\n   - `i` and `j` are initialized to 0.\n   - Iterate through the array with the `j` pointer.\n   - Increment the count of `nums[j]` in the map `mpp`.\n   - While the number of distinct elements in the window (`mpp.size()`) is greater than `k`, move the `i` pointer forward and decrement the count of `nums[i]` in `mpp`. If the count becomes 0, remove `nums[i]` from `mpp`.\n   - Update the count by adding the length of the current subarray `(j - i) + 1`.\n   - Increment `j` to expand the window.\n\n2. `subarraysWithKDistinct`: This function calculates the number of subarrays with exactly `k` distinct elements by subtracting the count of subarrays with at most `k-1` distinct elements from the count of subarrays with at most `k` distinct elements.\n\n   - Calls `checkSubarrayAtmostK(nums, k)` to get the count of subarrays with at most `k` distinct elements.\n   - Calls `checkSubarrayAtmostK(nums, k-1)` to get the count of subarrays with at most `k-1` distinct elements.\n   - Returns the difference between the counts.\n\nOverall, this algorithm efficiently finds the number of subarrays with exactly `k` distinct elements by leveraging the count of subarrays with at most `k` distinct elements and those with at most `k-1` distinct elements.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n\n    int checkSubarrayAtmostK(vector<int>& nums, int k){\n        int n=nums.size();\n        int i=0,j=0,count=0;\n        unordered_map<int , int> mpp;\n\n        while(j<n){\n            mpp[nums[j]]++;\n\n\n            while(i<=j && mpp.size()>k ){\n\n                mpp[nums[i]]--;\n\n                if( mpp[nums[i]] == 0 )  mpp.erase(nums[i]);\n\n                i++;\n            }\n\n            count+=(j-i)+1;\n            j++;\n        }\n\n        return count;\n    }\n\n\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        return checkSubarrayAtmostK(nums, k)-checkSubarrayAtmostK(nums , k-1);\n    }\n};\n```
['Array', 'Hash Table', 'Sliding Window', 'C++']