JAVA Solution Explained in HINDI
https://youtu.be/AetMjV3PFYY\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote 
https://youtu.be/AetMjV3PFYY\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote the solution if you liked it.\n\nSubscribe link:- https://www.youtube.com/@reelcoding?sub_confirmation=1\n\nSubscribe Goal:- 300\nCurrent Subscriber:- 274\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int atMost(int[] nums, int k) {\n        HashMap<Integer, Integer> map = new HashMap<>();\n        int i = 0, j = 0, n = nums.length, ans = 0;\n\n        while (j < n) {\n            map.put(nums[j], map.getOrDefault(nums[j], 0) + 1);\n            while (map.size() > k && i <= j) {\n                map.put(nums[i], map.get(nums[i]) - 1);\n                if (map.get(nums[i]) == 0) map.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        int ans = atMost(nums, k) - atMost(nums, k - 1);\n        return ans;\n    }\n}\n```
['Java']