The solution you are looking for.
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\n# HEY GUYS UPVOTE \r\nOverall Logic:\r\n\r\n1. The main function (numberOfSubarrays
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n# HEY GUYS UPVOTE \r\n**Overall Logic:**\r\n\r\n1. The main function (`numberOfSubarrays`) finds the difference in the number of subarrays allowed with `k` odd numbers and `k-1` odd numbers.\r\n2. The `atmost` function efficiently counts subarrays with at most `k` odd numbers using a sliding window and keeping track of odd numbers encountered.\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\nThe provided code calculates the number of subarrays in an integer array `nums` that contain at most `k` odd numbers. It leverages two functions:\r\n\r\n**1. `numberOfSubarrays(int[] nums, int k)`:**\r\n\r\n- This is the main function that calculates the final result.\r\n- It calls the `atmost(nums, k)` function twice: once with `k` and once with `k-1`.\r\n- It then subtracts the result from `atmost(nums, k-1)` from `atmost(nums, k)`. This effectively calculates the difference in the number of subarrays allowed with `k` odd numbers and with `k-1` odd numbers. This difference represents the number of subarrays allowed with exactly `k` odd numbers.\r\n\r\n**2. `atmost(int[] nums, int k)`:**\r\n\r\n- This function calculates the number of subarrays in the `nums` array that contain at most `k` odd numbers.\r\n- It uses a sliding window approach with two pointers (`i` and `j`) to explore all possible subarrays.\r\n- `int countOdd = 0`: This variable keeps track of the number of odd numbers encountered within the current window.\r\n- The `while` loop iterates as long as `j` (end of the current window) is less than the array length (`n`).\r\n  - `if (nums[j] % 2 != 0)`: Checks if the current element (`nums[j]`) is odd.\r\n    - If it\'s odd:\r\n      - `countOdd++`: Increments `countOdd` to track the additional odd number.\r\n  - The inner `while` loop contracts the window from the left (using `i`) as long as `countOdd` is greater than the allowed limit (`k`).\r\n    - `if (nums[i] % 2 != 0)`: Checks if the element at the beginning of the window (`nums[i]`) is odd.\r\n      - If it\'s odd:\r\n        - `countOdd--`: Decrements `countOdd` as an odd number is being removed from the window.\r\n    - `i++`: Increments `i` to move the window one step to the right, effectively removing the element from the current window.\r\n- `ans += j - i + 1`: After potentially contracting the window, this line updates the `ans` variable to count the number of subarrays within the current valid window size (`j - i + 1`).\r\n- `j++`: Increments `j` to move the window one step to the right for exploring the next subarray in the next iteration of the outer `while` loop.\r\n\r\n\r\n\r\n# Complexity\r\n- Time complexity:\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\nThis approach provides a time-efficient solution (O(n)) for finding the desired subarrays.\r\n- Space complexity:\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\nO(1)\r\n# Code\r\n```\r\nclass Solution {\r\n    public int numberOfSubarrays(int[] nums, int k) {\r\n        return atmost(nums,k)-atmost(nums,k-1);\r\n    }\r\n\r\n    public int atmost(int [] nums, int k){\r\n        int i=0;\r\n        int j=0;\r\n        int ans=0;\r\n        int countOdd=0;\r\n        int n=nums.length;\r\n        while(j<n){\r\n            if (nums[j]%2!=0){\r\n                countOdd++;\r\n            }\r\n            while(countOdd>k){\r\n                if (nums[i]%2!=0){\r\n                    countOdd--;\r\n                }\r\n                i++;\r\n            }\r\n            ans+=j-i+1;\r\n            j++;\r\n        }\r\n        return ans;\r\n    }\r\n}\r\n```
['Sliding Window', 'Java']