C++ | Easy | Sliding-Window | Faster Than 80 %
Solution:: number of subarrays with  k  different integers = (number of subarrays with number of different integers less than equal to k ) - (number of subarray
Solution:: number of subarrays with  **k  different integers** = (number of subarrays with number of different integers **less than equal to k** ) - (number of subarrays with number of different integers **less than equal to k-1** )\n```\n//output of this function -->numbers of subarrays which have number of different integers less than equal to k\n    int fun(int k,vector<int> nums)\n    {\n        if(k==0) return 0;\n        unordered_map<int,int> mp;\n        int j=0;\n        int count=0;\n        int n=nums.size();\n        int total=0;\n        vector<int> freq(n+1,0);\n        for(int i=0;i<n;i++)\n        {\n            if(freq[nums[i]]==0) count++;\n            freq[nums[i]]++;\n            if(count<=k) total+=(i-j+1); // (i-j+1) = number of subarrays ending at i-th position which contains number of different integers less than or equal to k\n            else\n            {\n                while(count>k)\n                {\n                    freq[nums[j]]--;\n                    if(freq[nums[j]]==0)\n                    {\n                        count--;\n                    }\n                    j++;\n                }\n                total+=(i-j+1);\n            }\n        }\n        return total;\n    }\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n       return fun(k,nums)-fun(k-1,nums);\n    }\n```\t\nIf you liked the Code, Give it a **Thumbs Up** !!!
['C', 'Sliding Window']