C++: Visual explanation. O(1) space. Two pointers
Algorithm\nThe best way to explain this approach is to look at the example:\nLet\'s say k = 2 and we have the following array:\n\n\n\nWe only going to work with
##### Algorithm\nThe best way to explain this approach is to look at the example:\nLet\'s say **k = 2** and we have the following array:\n\n![image](https://assets.leetcode.com/users/andnik/image_1582094442.png)\n\nWe only going to work with numbers `1` and `2` because we are only interested in if number is odd or even, we don\'t care what the actual value is.\n1. Using `i` iterate over the array counting **only odd** numbers:\n\n![image](https://assets.leetcode.com/users/andnik/image_1582094637.png)\n\n2. When **odd** is equal to **k**, we can analyse our **left part** and count how many subarrays with **odd == k** we can produce.\nWe are doing it with `j` iterating until we get to **odd number**.\n\n![image](https://assets.leetcode.com/users/andnik/image_1582094795.png)\n\nWe can see that there are `3` subarrays we can produce with **odd == k**.\n\n3. Continue with `i` again. Since **odd >= k** then every next **even number** we meet is going to **double previous count**. Let\'s see:\n\n![image](https://assets.leetcode.com/users/andnik/image_1582095218.png)\n\n4. When we meet **odd number** again we need to reset `count=1` and repeat step (2) with `j` again. In our example there\'s going to be only one subarray:\n\n![image](https://assets.leetcode.com/users/andnik/image_1582095418.png)\n\n5. And finish with counting last **even number** as in step (3).\n\nSince we are reseting `count` variable to 0, we are adding sum of all subarrays in `total` variable.\n\n##### Code\n```\nclass Solution {\npublic:\n    int numberOfSubarrays(vector<int>& nums, int k) {\n        int j = 0, odd = 0, count = 0, total = 0;\n        for (int i = 0; i < nums.size(); i++) {\n            if (nums[i] & 1) {\n                odd++;\n                if (odd >= k) {\n                    count = 1;\n                    while (!(nums[j++] & 1)) count++;\n                    total += count;\n                }\n            } else if (odd >= k) total += count;\n        }\n        return total;\n    }\n};\n```
['C']