Easy way in cpp  Subarrays with K Different Integers
\nclass Solution {\npublic:\n\n  int atMostK(vectorarr, int n, int k)\n{\n    int i = 0,j=0,count=0;\n     unordered_mapmp;\n        while (j k) {\n            
\nclass Solution {\npublic:\n\n  int atMostK(vector<int>arr, int n, int k)\n{\n    int i = 0,j=0,count=0;\n     unordered_map<int,int>mp;\n        while (j<n) {\n \n        if (mp.find(arr[j])==mp.end())\n            mp[arr[j]]=0;\n        mp[arr[j]]++;\n\n        while (mp.size() > k) {\n            mp[arr[i]]--;\n            if (mp[arr[i]] == 0)\n                mp.erase(arr[i]);\n        i++;\n        }\n        count += j - i+ 1;\n        j++;\n    }\n    return count;\n}\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n    int n=nums.size();\n  return (atMostK(nums, n, k) - atMostK(nums, n, k - 1));\n    }\n};\n```
['C++']