Solution using hashmap and some math
Intuition\n Describe your first thoughts on how to solve this problem. \nInitially thought sliding window but it is a complex one. After a bit of thinking I cam
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nInitially thought sliding window but it is a complex one. After a bit of thinking I came up with this mathy approach which is hopefully easier to understand.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nConvert the nums array to a prefix count of how many odds seen so far\nFor example:\n nums = [2,2,2,1,2,2,1,2,2,2]\nwill convert to \n\nnums = [0 0 0 1 1 1 2 2 2 2]\n\nwhere nums[i] now represents number of odds seen up until index i\n\nNow we can count how many odds seen, for example\n\nodds_seen :  count\n0:3\n1:3\n2:4\n\ngo through each odds_seen and see if you can create a subarray with k odds seen\n\nFor example for each odds_seen of 0 we have a count of 4 for (odd_seen+k = 0+2 = 2)\n\nTherefore we can make 3 (count of 0s) x 4 (count of 2s) = 12\nAnd we have odds_seen of 4 from the full array \nSo 4 + 12 = 16\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n# Code\n```\nclass Solution:\n    def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n        count_map = defaultdict(int)\n\n        odd_count = 0\n        for i in range(len(nums)):\n            if nums[i] % 2 == 1:\n                odd_count += 1\n                nums[i] = odd_count\n            count_map[odd_count] += 1\n\n        res = count_map[k]\n        for cnt in count_map:\n            if (cnt + k) in count_map:\n                res += count_map[cnt] * count_map[cnt+k]\n        \n        return res\n```
['Python3']