Easy C++ solution || Beginner Friendly
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\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:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int countSub(vector<int>& nums, int k) {\n        int l = 0, r = 0, count = 0;\n        unordered_map<int, int> mp;\n        while(r < nums.size()) {\n            mp[nums[r++]]++;\n            while(mp.size() > k) {\n                mp[nums[l]]--;\n                if(mp[nums[l]] == 0)\n                    mp.erase(nums[l]);\n                l++;\n            }\n            count += r - l;\n        }\n        return count;\n    }\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        return countSub(nums, k) - countSub(nums, k - 1);\n    }\n};\n```
['Hash Table', 'Two Pointers', 'Sliding Window', 'C++']