Lol first hard question done by myself ,so happy
\nclass Solution {\npublic:\n  int solve(vector<int>&nums,int k)\n  {\n    int n=nums.size();\n     int i=0;\n     int j=0;\n      int cnt=0;\n     map<int,int>
```\nclass Solution {\npublic:\n  int solve(vector<int>&nums,int k)\n  {\n    int n=nums.size();\n     int i=0;\n     int j=0;\n      int cnt=0;\n     map<int,int>mp;\n     while(j<n)\n     {\n       mp[nums[j]]++;\n       while(i<n && mp.size()>k)\n       {\n         mp[nums[i]]--;\n         if(mp[nums[i]]==0)\n         {\n           mp.erase(nums[i]);\n         }\n         i++;\n       }\n       cnt+=(j-i+1);\n       j++;\n     }\n      return cnt;\n  }\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n     return solve(nums,k)-solve(nums,k-1);// first i counted k and less than k  no integer and then i //counted k-1 and less than k-1; And to get exactly k we need to subtract \n\t\n    }\n};\n```
['Sliding Window']