Hash Maps to Find Nice Subarrays.
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves counting the number of subarrays that contain exactly \uD835\uDC58
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves counting the number of subarrays that contain exactly \uD835\uDC58.\n\nk odd numbers. Initially, this may seem complex due to the need to evaluate every possible subarray. However, leveraging prefix sums and hash maps can simplify this task significantly. The key observation is that the number of subarrays with exactly \uD835\uDC58.\n\nk odd numbers can be efficiently tracked using a sliding window approach or prefix sums to avoid recomputing sums repeatedly.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nPrefix Sum with Odd Counts: Transform the problem into one of counting prefix sums. Keep a running count of the number of odd numbers encountered so far. Use a hash map to store the frequency of each odd count encountered.\nHash Map for Efficient Counting: For each position in the array, check if the count of odd numbers minus \uD835\uDC58.\n\nk exists in the hash map. If it does, it means there exists a subarray ending at the current position with exactly \uD835\uDC58.\n\nIterate and Update Hash Map: As we iterate through the array, we update the hash map with the current count of odd numbers. This allows us to efficiently find the number of subarrays that end at the current position and contain exactly \uD835\uDC58.\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 numberOfSubarrays(int[] nums, int k) {\n        int n = nums.length;\n        int[] cnt = new int[n + 1];\n        cnt[0] = 1;\n        int ans = 0, t = 0;\n        for (int v : nums) {\n            t += v & 1;\n            if (t - k >= 0) {\n                ans += cnt[t - k];\n            }\n            cnt[t]++;\n        }\n        return ans;\n    }\n}\n```
['Array', 'Hash Table', 'Math', 'Sliding Window', 'C++', 'Java']