C++ Explanation||Sliding Window||Easy to Understand
-> This problem will be a very typical sliding window,\n-> if it asks the number of subarrays with at most K distinct elements.\n\n-> Just need one more step to
-> This problem will be a very typical sliding window,\n-> if it asks the number of subarrays with at most K distinct elements.\n\n-> Just need one more step to reach the folloing equation:\n-> exactly(K) = atMost(K) - atMost(K-1)\n```    \nclass Solution {\npublic:\n    int solve(vector<int>& nums, int k)\n    {\n        int i=0,res=0;\n        unordered_map<int,int> m;\n        for(int j=0;j<nums.size();j++)\n        {\n            if(!m[nums[j]]++)\n                k--;\n            while(k<0)\n            {\n                if(!--m[nums[i]])\n                    k++;\n                i++;\n            }\n            res+=j-i+1;\n        }\n        return res;\n        \n    }\n    int subarraysWithKDistinct(vector<int>& nums, int k)\n    {\n        return solve(nums,k)-solve(nums,k-1); //(1 to k all)-(1 to k-1 all)==exactly k  \n        \n    }\n};\n//if you like the solution plz upvote.
['C', 'Sliding Window']