CPP INTITUTIVE | SLIDING WINDOW | UNORDERED_MAP | SHORT
Main idea is we can easly calculate number of different subarrays having at most k distant elements instead of exact k elements so we just exploite normal math 
Main idea is we can easly calculate number of different subarrays having at most k distant elements instead of exact k elements so we just exploite normal math atMostK( k ) - atMost( k-1 ).\n```\nclass Solution {\npublic:\n    int help(vector<int>&a , int k){\n        if( k == 0){\n            return 0;\n        }\n        unordered_map<int,int> map;\n        int n = a.size();\n        int j=0;\n        int ans = 0;\n        for(int i=0;i<n;i++){\n            map[a[i]]++;\n            while(map.size() > k and j <= i){\n                int p = map[a[j]]--;\n                if(p == 1){\n                    map.erase(a[j]);\n                }\n                j++;\n            }\n            ans += (i-j+1);\n        }\n        return ans;\n    }\n    int subarraysWithKDistinct(vector<int>& a, int k) {\n        return help(a, k) - help(a,k-1);\n    }\n};\n```
[]