WELL-EXPLAINED🧠 EASIEST SOLUTION🧐 SLIDING WINDOW🪟TWO-POINTERS
UPVOTE IF YOU FIND THE SOLUTION HELPFUL\uD83D\uDCA1\n\n# Intuition\nThe problem requires us to find the number of continuous subarrays with exactly k odd number
# ***UPVOTE IF YOU FIND THE SOLUTION HELPFUL\uD83D\uDCA1***\n\n# Intuition\nThe problem requires us to find the number of continuous subarrays with exactly k odd numbers. This can be efficiently solved using a `sliding window` with `two-pointer` approach.\n\n\n# Approach\n1. **Initialization:**\n   - Initialize two pointers, `i` and `j`, both set to the start of the array. These pointers define the current window of elements being considered.\n   - `oddCount` keeps track of the number of odd numbers within the current window.\n   - `subArrayCount` keeps the count of nice subarrays.\n   - `temp` temporarily stores the number of valid subarrays ending at the current position `j`.\n\n2. **Sliding Window Expansion:**\n   - Traverse the array using pointer `j`. For each element, check if it is odd (`nums[j] % 2 == 1`). If it is odd, increment `oddCount` and reset `temp` to 0.\n   - If `oddCount` equals `k`, it means the current window has exactly `k` odd numbers. Start shrinking the window from the left to explore the further possible **Nice sub-arrays**, so (increment `i`) while `oddCount` remains `k`:\n     - Increment `temp` for each valid subarray found by moving `i`.\n     - If the element at `i` is odd, decrement `oddCount` as it will be excluded from the window.\n   - Add `temp` to `subArrayCount` for each valid subarray found ending at the current `j`. In this way, we can find all the possible **Nice sub-arrays** ending at the index `j`\n\n3. **Return Result:**\n   - After processing all elements, `subArrayCount` will contain the total number of nice subarrays.\n\n# Code\n``` Java []\nclass Solution {\n    public int numberOfSubarrays(int[] nums, int k) {\n       int i = 0, j = 0;\n       int oddCount = 0;\n       int subArrayCount = 0;\n       int temp = 0;\n\n       while(j < nums.length){\n        if(nums[j] % 2 == 1){\n            oddCount++;\n            temp = 0;\n        }\n\n        while(oddCount == k){\n            temp++;\n            if(nums[i] % 2 == 1){\n                oddCount--;\n            }\n            i++;\n        }\n        subArrayCount += temp;\n        j++;\n       }\n       return subArrayCount;\n    }\n}\n```\n``` Python []\nclass Solution:\n    def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n        i = 0\n        j = 0\n        odd_count = 0\n        sub_array_count = 0\n        temp = 0\n\n        while j < len(nums):\n            if nums[j] % 2 == 1:\n                odd_count += 1\n                temp = 0\n            \n            while odd_count == k:\n                temp += 1\n                if nums[i] % 2 == 1:\n                    odd_count -= 1\n                i += 1\n            \n            sub_array_count += temp\n            j += 1\n        \n        return sub_array_count\n```\n``` C++ []\nclass Solution {\npublic:\n    int numberOfSubarrays(vector<int>& nums, int k) {\n        int i = 0, j = 0;\n        int oddCount = 0;\n        int subArrayCount = 0;\n        int temp = 0;\n\n        while (j < nums.size()) {\n            if (nums[j] % 2 == 1) {\n                oddCount++;\n                temp = 0;\n            }\n\n            while (oddCount == k) {\n                temp++;\n                if (nums[i] % 2 == 1) {\n                    oddCount--;\n                }\n                i++;\n            }\n            subArrayCount += temp;\n            j++;\n        }\n        return subArrayCount;\n    }\n};\n```\n``` JavaScript []\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar numberOfSubarrays = function(nums, k) {\n    let i = 0, j = 0;\n    let oddCount = 0;\n    let subArrayCount = 0;\n    let temp = 0;\n\n    while(j < nums.length) {\n        if(nums[j] % 2 === 1) {\n            oddCount++;\n            temp = 0;\n        }\n\n        while(oddCount === k) {\n            temp++;\n            if(nums[i] % 2 === 1) {\n                oddCount--;\n            }\n            i++;\n        }\n        subArrayCount += temp;\n        j++;\n    }\n    return subArrayCount;\n};\n\n```\n\n![upvote.jpg](https://assets.leetcode.com/users/images/bdd0807c-3eef-4353-addc-f1101bc74aed_1719046538.9723315.png)\n
['Array', 'Hash Table', 'Math', 'Two Pointers', 'Sliding Window', 'Python', 'C++', 'Java', 'JavaScript']