c++ code easy solution using map and sliding window with approach
Approach: To directly count the subarrays with exactly K different integers is hard but to find the count of subarrays with at most K different integers is easy
Approach: To directly count the subarrays with exactly K different integers is hard but to find the count of subarrays with at most K different integers is easy. So the idea is to find the count of subarrays with at most K different integers, let it be C(K), and the count of subarrays with at most (K \u2013 1) different integers, let it be C(K \u2013 1) and finally take their difference, C(K) \u2013 C(K \u2013 1) which is the required answer.\nCount of subarrays with at most K different elements can be easily calculated through the sliding window technique. The idea is to keep expanding the right boundary of the window till the count of distinct elements in the window is less than or equal to K and when the count of distinct elements inside the window becomes more than K, start shrinking the window from the left till the count becomes less than or equal to K. Also for every expansion, keep counting the subarrays as right \u2013 left + 1 where right and left are the boundaries of the current window.\n```\nint atMostKDistinct(vector<int>& A,int n,int k){\n    int count = 0;\n    int left = 0;\n    int right = 0;\n    \n    unordered_map<int,int> mp;\n    \n    while(right < n){\n        \n        //add current element in the map\n        if(mp.find(A[right]) == mp.end()){\n            mp[A[right]] = 1;\n        }else{\n            mp[A[right]]++;\n        }\n        \n        while(mp.size() > k){\n            mp[A[left]]--;\n            if(mp[A[left]] == 0){\n                mp.erase(A[left]);\n            }\n            left++;\n        }\n        \n        count += right-left+1;\n        right++;\n    }\n    \n    \n    return count;\n}\n\n\nclass Solution {\npublic:\n    int subarraysWithKDistinct(vector<int>& A, int k) {\n        int n = A.size();\n        return atMostKDistinct(A,n,k) - atMostKDistinct(A,n,k-1);\n    }\n};\n```\nhope it helps
['C']