💣EASY CODE ✅& FULLY EXPLAINED✨||BEST APPROACH🔥
Intuition:\nImagine you have a bunch of candies in different colors, and you want to count how many ways you can pick K candies where each candy is a different 
# Intuition:\nImagine you have a bunch of candies in different colors, and you want to count how many ways you can pick K candies where each candy is a different color.\n\n# Approach :\n# Step 1: Counting Different Groups\n- We start picking candies from the beginning of our collection.\n- We keep track of how many different colors of candies we have using a special counting tool.\n- We also have two markers, one for the start and one for the end of our pick.\n```\nwhile (j < n) {\n            mpp[nums[j]]++;\n            //some code\n            j++;\n}\n```\n# Step 2: Finding Different Groups\n- We keep picking candies, adding new colors to our collection and updating our counting tool.\n- If we have more than K different colors, we remove some candies from the start until we have exactly K different colors again.\n- While doing this, we count how many different sets of candies we can make.\n```\nwhile ( mpp.size() > k) {\n                mpp[nums[i]]--;\n                if (mpp[nums[i]] == 0) mpp.erase(nums[i]);\n                i++;\n            }\n\n```\n# Step 3: Calculating the Total\n- We keep track of the total number of different sets we find as we go through all the candies.\n```\nans += j - i +1;\n```\n# Step 4: Subtracting to Get the Final Answer\n- We repeat the same process, but this time with K-1 different colors.\n- By subtracting the count of sets with at most K\u22121 different colors from the count of sets with exactly K different colors, we are left with the count of sets having exactly K different colors.\n```\nint subarraysWithKDistinct(vector<int>& nums, int k) {\n        return solve(nums,k)-solve(nums,k-1);\n    }\n```\n# Complexity:\n**Time Complexity:**\nThe time it takes to do all these steps depends on how many candies we have which is O(N).\n**Space Complexity:**\nThe space we need to store our counting tool and markers also depends on the number of candies we have which is again O(N).\n\n# C++\n# Code\n```\n#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);\nclass Solution {\n    protected:\n    int solve(vector<int>& nums, int k)\n    {\n        int n = nums.size();\n        int ans = 0;\n        unordered_map<int, int> mpp;\n        int j = 0, i = 0;\n        while (j < n) {\n            mpp[nums[j]]++;\n            while ( mpp.size() > k) {\n                mpp[nums[i]]--;\n                if (mpp[nums[i]] == 0) mpp.erase(nums[i]);\n                i++;\n            }\n            ans += j - i +1;\n            j++;\n        }\n        return ans;\n    }\npublic:\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        return solve(nums,k)-solve(nums,k-1);\n    }\n};\n\n```\n# JAVA\n```\nimport java.util.HashMap;\n\nclass Solution {\n    protected int solve(int[] nums, int k) {\n        int n = nums.length;\n        int ans = 0;\n        HashMap<Integer, Integer> mpp = new HashMap<>();\n        int j = 0, i = 0;\n        while (j < n) {\n            mpp.put(nums[j], mpp.getOrDefault(nums[j], 0) + 1);\n            while (mpp.size() > k) {\n                mpp.put(nums[i], mpp.get(nums[i]) - 1);\n                if (mpp.get(nums[i]) == 0) mpp.remove(nums[i]);\n                i++;\n            }\n            ans += j - i + 1;\n            j++;\n        }\n        return ans;\n    }\n\n    public int subarraysWithKDistinct(int[] nums, int k) {\n        return solve(nums, k) - solve(nums, k - 1);\n    }\n}\n\n```\n# PYTHON\n```\nclass Solution:\n    def solve(self, nums, k):\n        n = len(nums)\n        ans = 0\n        mpp = {}\n        i = j = 0\n        while j < n:\n            mpp[nums[j]] = mpp.get(nums[j], 0) + 1\n            while len(mpp) > k:\n                mpp[nums[i]] -= 1\n                if mpp[nums[i]] == 0:\n                    del mpp[nums[i]]\n                i += 1\n            ans += j - i + 1\n            j += 1\n        return ans\n\n    def subarraysWithKDistinct(self, nums, k):\n        return self.solve(nums, k) - self.solve(nums, k - 1)\n\n```
['Array', 'Hash Table', 'Two Pointers', 'Sliding Window', 'Counting', 'Java', 'Python3']