C++ | easy sol using Sliding window
Approach: we need exactly k elements so we need to remove less than k elements from (at most k elements)\nIf the question had been return at most k size good ar
**Approach:** we need exactly k elements so we need to remove less than k elements from (at most k elements)\nIf the question had been return at most k size good array\nwe don\'t need to have to do this step of subtracting less than k elements\n```\nclass Solution {\npublic:\n    int helper(vector<int>& nums, int k)\n    {\n        unordered_map<int,int> mp;\n        int n = nums.size();\n        int i=0;\n        int unique=0;//unique no till the current window\n        int ans=0;\n        for(int j=0; j<n; j++)\n        {\n            int curr = nums[j];\n            if(mp.find(curr) == mp.end())\n                unique++;\n            \n            mp[curr]++;\n            \n            while(unique > k)\n            {\n                mp[nums[i]]--;\n                if(mp[nums[i]] == 0)\n                {\n                    mp.erase(nums[i]);\n                    unique--;\n                }\n                \n                i++;\n            }\n            ans += j-i+1;\n        }\n        return ans;\n    }\n    int subarraysWithKDistinct(vector<int>& nums, int k) \n    {\n        return helper(nums,k) - helper(nums, k-1);\n    }\n};
['C', 'Sliding Window']