Modifying the question (Making it much more easier) || Sliding window || Easy to Understand
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\nTreat odd numbers as 1 and even numbers as 0, then solve as if you want sum of suba
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nTreat odd numbers as 1 and even numbers as 0, then solve as if you want sum of subarray having sum = k.\r\n\r\nSame as this question: [930. Binary Subarrays With Sum\r\n](https://leetcode.com/problems/binary-subarrays-with-sum/)\r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n1. Replace all the odd elements with 1 and all the even elements with 0.\r\n2. Now calculate the subarrays with sum = k.\r\n3. This can be done by finding the subarrays with **sum <= k** and finding subarrays with **sum <= k - 1** and subtrack later from the previous one.\r\n\r\n**Feel free to post your doubts in the comments :)**\r\n\r\n# Complexity\r\n- Time complexity: **O(n)**\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity: **O(1)**\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```\r\nclass Solution {\r\npublic:\r\n    int atMost(vector<int>& nums, int k)\r\n    {\r\n        int left = 0;\r\n        int right = 0;\r\n        int ans = 0;\r\n\r\n        for(right = 0; right < nums.size(); right++)\r\n        {\r\n            k -= nums[right];\r\n            while(k < 0)\r\n            {\r\n                k += nums[left];\r\n                left++;\r\n            }\r\n            ans += right - left + 1;\r\n        }\r\n        return ans;\r\n    }\r\n    int numberOfSubarrays(vector<int>& nums, int k) {\r\n        for(int i = 0; i < nums.size(); i++)\r\n        {\r\n            if(nums[i] % 2 == 1) nums[i] = 1;\r\n            else nums[i] = 0;\r\n        }\r\n        return atMost(nums, k) - atMost(nums, k - 1);\r\n    }\r\n};\r\n```
['Sliding Window', 'Python', 'C++', 'Java']