Sliding Window ✅ Easy
Similar ::\n2799. Count Complete Subarrays in an Array\n>>> https://leetcode.com/problems/count-complete-subarrays-in-an-array/solutions/3891740/sliding-window-
**Similar ::**\n***2799. Count Complete Subarrays in an Array***\n**>>>** https://leetcode.com/problems/count-complete-subarrays-in-an-array/solutions/3891740/sliding-window-easy/\n\n________________________________\n\n# Approach  : at most *K* subsets\n```\nclass Solution {\n     public int subarraysWithKDistinct(int[] nums, int k) {\n         return f(nums,k)-f(nums,k-1);\n     }\n    public int f(int[] nums, int k) {\n        HashMap<Integer,Integer> map=new HashMap<>();\n        int si=0;\n        int ei=0;\n        int count=0;\n        while(ei<nums.length){\n            map.put(nums[ei], map.getOrDefault(nums[ei], 0) + 1);\n            while(map.size()>k){\n\t\t\t\tif(map.get(nums[si]) > 1) {\n\t\t\t\t\tmap.put(nums[si], map.get(nums[si]) - 1);\n\t\t\t\t} else {\n\t\t\t\t\tmap.remove(nums[si]);\n\t\t\t\t}\n               si++;\n            }\n            count+=ei-si+1;\n            ei++;\n        }\n        return count;\n    }\n}\n```\n________________________________\n\n**Upvote if helps**\n![image.png](https://assets.leetcode.com/users/images/f18ccf7b-bb7d-4a8e-8622-2f0acbd705cc_1683886081.2272806.png)\n\n________________________________
['Sliding Window', 'Hash Function', 'Java']