Easy Sliding Window approach in C++ (O(n) time complexity and O(n) space complexity)
\nclass Solution \n{\npublic:\n    // Helper function to determine number of subarrays with at most k distinct integers\n    int helper(vector<int>&nums,int k)\
```\nclass Solution \n{\npublic:\n    // Helper function to determine number of subarrays with at most k distinct integers\n    int helper(vector<int>&nums,int k)\n    {\n        int i=0,j=0,count=0,n=nums.size();\n        unordered_map<int,int>m;\n        while(j<n)\n        {\n            m[nums[j]]++;\n            while(i<=j && m.size()>k)\n            {\n                if(m[nums[i]]==1)\n                {\n                    m.erase(nums[i]);\n                }\n                else\n                {\n                    m[nums[i]]--;\n                }\n                i++;\n            }\n            count+=(j-i+1);\n            j++;\n        }\n        return count;\n    }\n    int subarraysWithKDistinct(vector<int>& nums, int k) \n    {\n        // The concept:-->\n        // helper(nums,k)=number of subarrays with at most k distinct integers\n        // helper(nums,k-1)=number of subarrays with at most k-1 distinct integers\n        // So more formally we can say that \n        // helper(nums,k)=number of subarrays with exactly k distinct integers + number of subarrays with less than k distinct integers\n        // So if we exclude the number of subarrays with less than k distinct integers from the number of subarrays with at most k distinct integers, we\'ll get our answer.\n        return helper(nums,k)-helper(nums,k-1);\n    }\n};\n```\n**Don\'t forget to upvote if you\'ve got any help from my explanation.**
['C', 'Sliding Window']