Sliding window
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:o(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:o(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int numberOfSubarrays(vector<int>& nums, int k) {\n\n        int i = 0, a = -1, b = -1, n = nums.size(), cnt = 0, ans = 0;\n\n         // First, find the first window that contains exactly k odd numbers\n        while (cnt < k && i < n) {\n            if (nums[i] % 2 == 1) { // Check if the number is odd\n                cnt++;              // Increment the odd count\n                if (b == -1)        // Set the initial right boundary\n                    b = i;\n            }\n            i++;\n        }\n\n        // If there are not enough odd numbers to make a window of k odd numbers\n        if (cnt < k)\n            return 0;\n        \n        // Calculate the number of subarrays for the initial window\n        ans = b - a;\n\n        // Continue iterating through the rest of the array\n        while (i < n) {\n            if (nums[i] % 2 == 1) { // Check if the current number is odd\n                a = b;              // Move the left boundary to the previous right boundary\n                b++;\n                while (b < n) {\n                    if (nums[b] % 2 == 1) // Find the next odd number\n                        break;\n                    b++;\n                }\n            }\n            ans += b - a; // Add the number of valid subarrays ending at the current position\n            i++;\n        }\n        return ans; // Return the total number of valid subarrays\n    }\n};\n```\n![image.png](https://assets.leetcode.com/users/images/187f2c1a-ca96-48ce-8818-bfb0c10e4ac3_1719016399.1251187.png)\n
['Array', 'Sliding Window', 'C++']