💯✅🔥Easy Java ,Python3 ,C++ Solution|| 15 ms ||≧◠‿◠≦✌
Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea is to count the number of subarrays with exactly k odd numbers. This is done b
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea is to count the number of subarrays with exactly k odd numbers. This is done by maintaining a sliding window of odd numbers and incrementing the count whenever the window size is equal to k. The subarray function calculates the total number of subarrays with k or fewer odd numbers, and the numberOfSubarrays function subtracts the number of subarrays with k-1 or fewer odd numbers to get the number of subarrays with exactly k odd numbers.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe code uses a two-pointer technique to solve the problem. It maintains two pointers, l and r, which represent the start and end of the current subarray. It also keeps track of the number of odd numbers in the current subarray using the variable odd.\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\n```java []\npublic class Solution {\n    public int numberOfSubarrays(int[] nums, int k) {\n        int n = nums.length;\n        int l = 0, r = 0, odd = 0, count = 0;\n\n        while (r < n) {\n            if (nums[r] % 2 != 0) {\n                odd++;\n            }\n            while (odd > k) {\n                if (nums[l] % 2 != 0) {\n                    odd--;\n                }\n                l++;\n            }\n            count += r - l + 1;\n            r++;\n        }\n        return count - subarray(nums, k - 1);\n    }\n\n    public int subarray(int[] nums, int k) {\n        if (k < 0) {\n            return 0;\n        }\n        int n = nums.length;\n\n        int l = 0, r = 0, odd = 0, count = 0;\n\n        while (r < n) {\n            if (nums[r] % 2 != 0) {\n                odd++;\n            }\n            while (odd > k) {\n                if (nums[l] % 2 != 0) {\n                    odd--;\n                }\n                l++;\n            }\n            count += r - l + 1;\n            r++;\n        }\n        return count;\n    }\n}\n```\n```python3 []\nclass Solution:\n    def numberOfSubarrays(self, nums, k):\n        n = len(nums)\n        l = 0\n        r = 0\n        odd = 0\n        count = 0\n\n        while r < n:\n            if nums[r] % 2 != 0:\n                odd += 1\n            while odd > k:\n                if nums[l] % 2 != 0:\n                    odd -= 1\n                l += 1\n            count += r - l + 1\n            r += 1\n        return count - self.subarray(nums, k - 1)\n\n    def subarray(self, nums, k):\n        if k < 0:\n            return 0\n        n = len(nums)\n\n        l = 0\n        r = 0\n        odd = 0\n        count = 0\n\n        while r < n:\n            if nums[r] % 2 != 0:\n                odd += 1\n            while odd > k:\n                if nums[l] % 2 != 0:\n                    odd -= 1\n                l += 1\n            count += r - l + 1\n            r += 1\n        return count\n```\n```C++ []\nclass Solution {\npublic:\n    int numberOfSubarrays(std::vector<int>& nums, int k) {\n        int n = nums.size();\n        int l = 0, r = 0, odd = 0, count = 0;\n\n        while (r < n) {\n            if (nums[r] % 2 != 0) {\n                odd++;\n            }\n            while (odd > k) {\n                if (nums[l] % 2 != 0) {\n                    odd--;\n                }\n                l++;\n            }\n            count += r - l + 1;\n            r++;\n        }\n        return count - subarray(nums, k - 1);\n    }\n\n    int subarray(std::vector<int>& nums, int k) {\n        if (k < 0) {\n            return 0;\n        }\n        int n = nums.size();\n\n        int l = 0, r = 0, odd = 0, count = 0;\n\n        while (r < n) {\n            if (nums[r] % 2 != 0) {\n                odd++;\n            }\n            while (odd > k) {\n                if (nums[l] % 2 != 0) {\n                    odd--;\n                }\n                l++;\n            }\n            count += r - l + 1;\n            r++;\n        }\n        return count;\n    }\n};\n```\n\n![upvote.png](https://assets.leetcode.com/users/images/6b58bf92-2bee-466f-a202-b2d19220c8b5_1719016979.519272.png)\n
['Array', 'C++', 'Java', 'Python3']