✅Beats 100% | Variable Size Sliding Window | Easy Explaination
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\nThe task is to find the number of subarrays with exactly k odd numbers. To achieve 
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nThe task is to find the number of subarrays with exactly k odd numbers. To achieve this, we use a helper function to count the number of subarrays with at most k odd numbers. The difference between the number of subarrays with at most k odd numbers and the number of subarrays with at most k-1 odd numbers gives us the number of subarrays with exactly k odd numbers.\r\n\r\nExactly `K` times = at most `K` times - at most `K - 1` times\r\n\r\n# Java Code\r\n```\r\nclass Solution {\r\n    public int numberOfSubarrays(int[] nums, int k) {\r\n        \r\n        //SubArrays which have exactly k odd numbers = total subArrays with k odd - totalSubArray with k-1 odd\r\n        return  findTotalSubArrays(nums, k) - findTotalSubArrays(nums, k - 1);\r\n    }\r\n    //this will find all the subarrays till k odd numbers\r\n    public int findTotalSubArrays(int[] nums, int k){\r\n\r\n        int i = 0;\r\n        int j = 0;\r\n        int subArrays = 0;\r\n        int countOdd = 0;\r\n\r\n        while(j < nums.length){\r\n\r\n            if(nums[j] % 2 != 0){   //calculation part\r\n                countOdd++;\r\n            }\r\n\r\n            while(countOdd > k){    //if !condition \r\n\r\n                if(nums[i] % 2 != 0){   //remove the calculation part using i\r\n                    countOdd--;\r\n                }\r\n                i++;    \r\n            }\r\n            subArrays += j - i + 1;     //find all subarrays\r\n            j++;\r\n        } \r\n        return subArrays;\r\n    }\r\n}\r\n```\r\n# C++ Code\r\n```\r\nclass Solution {\r\npublic:\r\n    int numberOfSubarrays(vector<int>& nums, int k) {\r\n        return findTotalSubArrays(nums, k) - findTotalSubArrays(nums, k - 1);\r\n    }\r\n\r\n    int findTotalSubArrays(vector<int>& nums, int k) {\r\n        int i = 0, j = 0, subArrays = 0, countOdd = 0;\r\n\r\n        while (j < nums.size()) {\r\n            if (nums[j] % 2 != 0) { // Calculation part\r\n                countOdd++;\r\n            }\r\n\r\n            while (countOdd > k) { // if condition fails\r\n                if (nums[i] % 2 != 0) { // Remove the calculation part using i\r\n                    countOdd--;\r\n                }\r\n                i++;\r\n            }\r\n\r\n            subArrays += j - i + 1; // Find all subarrays\r\n            j++;\r\n        }\r\n        return subArrays;\r\n    }\r\n};\r\n\r\n```\r\n# Python Code\r\n```\r\nclass Solution:\r\n    def numberOfSubarrays(self, nums: List[int], k: int) -> int:\r\n        return self.findTotalSubArrays(nums, k) - self.findTotalSubArrays(nums, k - 1)\r\n    \r\n    def findTotalSubArrays(self, nums: List[int], k: int) -> int:\r\n        i = 0\r\n        j = 0\r\n        subArrays = 0\r\n        countOdd = 0\r\n        \r\n        while j < len(nums):\r\n            if nums[j] % 2 != 0:  # Calculation part\r\n                countOdd += 1\r\n            \r\n            while countOdd > k:  # if condition fails\r\n                if nums[i] % 2 != 0:  # Remove the calculation part using i\r\n                    countOdd -= 1\r\n                i += 1\r\n            \r\n            subArrays += j - i + 1  # Find all subarrays\r\n            j += 1\r\n        \r\n        return subArrays\r\n\r\n```
['Array', 'Hash Table', 'Math', 'Sliding Window', 'Python', 'C++', 'Java']