Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.

Return the number of nice sub-arrays.

&nbsp;
Example 1:

Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].


Example 2:

Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.


Example 3:

Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 50000
	1 &lt;= nums[i] &lt;= 10^5
	1 &lt;= k &lt;= nums.length

class Solution(object):
	def numberOfSubarrays(self, nums, k):
		"""
		e.g. k = 2
		nums = [2, 2, 1, 2, 1, 2, 2]
		index=  0  1  2  3  4  5  6
		2 even numbers to left of first 1
		2 even numbers to right of last 1
		total number of subarrays = pick between 0-2 numbers on left, then, pick between 0-2 numbers on right
		i.e (left+1)  (right+1)
		Then slide window to next set of 2 odd numbers
		"""

		odds = []

		for i in range(len(nums)):
			if nums[i] & 1:
				odds.append(i)                      #' Find index of all odd numbers '

		odds = [-1] + odds + [len(nums)]            #' Handle edge cases '
		nice = 0

		for i in range(1, len(odds)-k):
			left = odds[i] - odds[i-1] - 1          #' Number of 'left' even numbers '
			right = odds[i+k] - odds[i+k-1] - 1     #' Number of 'right' even numbers '
			nice += (left+1)*(right+1)              #' Total sub-arrays in current window '

		return nice
    
class Solution {
    public int numberOfSubarrays(int[] nums, int k) {
        int i = 0;
        int j = 0;
        int odd = 0;
        int result = 0;
        int temp = 0;

        /*
            Approach : two pointer + sliding window technique

            step 1 : we have fix i and moving j until our count of odd numbers == k
            step 2 : when(odd == count) we are counting every possible subarray by reducing the size of subarray from i

        why temp?
        from reducing the size of subarray we will count all the possible subarray from between i and j
        but when i encounter a odd element the odd count will reduce and that while will stop executing

        now there are two possible cases
        1.The leftover elements have a odd number
        2.The leftover elements do not have any odd number

        1. if our leftover elements have a odd number
                then we cannot include our old possible subarrays into new possible subarrays because now new window for having odd == k is formed
                that's why temp = 0;

        2. if out leftover elements do not have any odd element left
            then our leftover elements must also take in consideration becuase they will also contribute in forming subarrays
        */
        while(j< nums.length){
            if(nums[j]%2 != 0)
            {
                odd++;
                //if leftover elements have odd element then new window is formed so we set temp = 0;
                temp = 0;
            }
            while(odd ==k){
                temp++;
                if(nums[i] %2 != 0)
                    odd--;
                i++;
             }
          //if no leftover elements is odd, each element will part in forming subarray
        //so include them
            result += temp;
            j++;

        }
        return result;
    }
}
class Solution {
public:
    vector<int> nums;
    int solve(int k){
        int low = 0, high = 0, cnt = 0, res = 0;

        while(high < nums.size()){
            if(nums[high] & 1){
                cnt++;

                while(cnt > k){
                    if(nums[low] & 1) cnt--;
                    low++;
                }
            }
            high++;
            res += high - low;
        }

        return res;
    }
    int numberOfSubarrays(vector<int>& nums, int k) {
        this -> nums = nums;
        return solve(k) - solve(k - 1);
    }
};
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var numberOfSubarrays = function(nums, k) {
    const len = nums.length;
    const pre = new Array(len).fill(-1);
    const post = new Array(len).fill(len);

    let lastOcc = -1;
    for(let i = 0; i < len; i++) {
        pre[i] = lastOcc;
        if(nums[i] & 1) lastOcc = i;
    }
    lastOcc = len;
    for(let i = len - 1; i >= 0; i--) {
        post[i] = lastOcc;
        if(nums[i] & 1) lastOcc = i;
    }

    let l = 0, r = 0, oddCount = 0;

    let ans = 0;

    for(; r < len; r++) {
        while(l < len && !(nums[l] & 1)) l++;
        if(nums[r] & 1) oddCount++;

        if(oddCount == k) {
            let leftCount = l - pre[l];
            let rightCount = post[r] - r;
            ans += leftCount * rightCount;

            oddCount--;
            l++;
        }
    }

    return ans;
};
Count Number of Nice Subarrays