Simple sliding window approach .. C++.. using atmost k distinct integers concept..
Intuition\n Describe your first thoughts on how to solve this problem. \nHere we can try to find ans for windows with atmost k distinct integers and atmost k-1 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHere we can try to find ans for windows with atmost k distinct integers and atmost k-1 distinct integers.. calculating this is easy and hence after subtracting both we can get windows with exactly k distinct integers .. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nafter generating windows for atmost k distinct integers ..\ntc: 1,2,1,2,3\nWindows with atmost 2 distinct integers :\n[1],[1,2],[1,2,1],[1,2,1,2],[2],[2,1],[2,1,2],[1],[1,2],[2],[2,3],[3]\n\nthese are 12 windows .. out of which 5 windows are useless to us for calculation for exactly 2 distinct\n\nwindows with 1 distinct integer .. which are useless:\n[1],[2],[1],[2],[3]\n\nremoving these will give us the correct ans..12-5=7\n\n\n\nplease upvote..\n\n\n\n\n# Complexity\n- Time complexity:\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    // calculation of atmost k distinct integers \n    int count(vector<int>& nums,int k){\n        int cnt=0;\n        // front and back pointers \n        int f=0;\n        int b=0;\n        int n=nums.size();\n        map<int,int> mp;\n\n        while(f<n){\n            // edge case for k=1 where back will exceed front pointer\n            if(b>f){\n                f=b;\n                mp.clear();\n                continue;\n            }\n            // adding to map \n            mp[nums[f]]++;\n            // satisfies .. which means all windows between f and b will add to cnt (f-b+1)\n            if(mp.size()<=k){\n                cnt+=(f-b+1);\n                f++;\n            }\n            // moving back pointer front and removing from map \n            else{\n                mp[nums[b]]--;\n                if(mp[nums[b]]==0){\n                    mp.erase(nums[b]);\n                }\n                b++;\n                // removing front also as next time it will be added again \n                mp[nums[f]]--;\n                if(mp[nums[f]]==0){\n                    mp.erase(nums[f]);\n                }\n\n            }\n\n        }\n        return cnt;\n\n    }\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        map<int,int> mp;\n        map<int,int> last;\n        int n=nums.size();\n\n        // doing count(k)-count(k-1);\n\n        int a=count(nums,k);\n        int b=count(nums,k-1);\n        cout<<a<<" "<<b<<endl;\n        return a-b;\n\n        \n    }\n};\n```
['C++']