> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/sliding-window/medium/1248.-count-number-of-nice-subarrays.md).

# 1248. Count Number of Nice Subarrays

## Problem Statement

<br>

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*.

&#x20;

**Example 1:**

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

**Example 2:**

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

**Example 3:**

<pre><code><strong>Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
</strong><strong>Output: 16
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= nums.length <= 50000`
* `1 <= nums[i] <= 10^5`
* `1 <= k <= nums.length`

## Intuition

```
Approach1:
Convert odd and even to 1/0 
And apply prefix sum using hash map

Similar to above problem

Approach 2:

Slinding window 
Atmost k - Atmost k-1 approach


```

### Links

<https://leetcode.com/problems/count-number-of-nice-subarrays/description/>

### Video Links

<https://www.youtube.com/watch?v=O0bbpT710KA&t=1s&ab_channel=CodingSamurai%27s>

### Approach 1:

```
MAp and Prefix sum
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int numberOfSubarrays(vector<int>& nums, int k) {
        for(int i=0; i<nums.size(); i++){
            if(nums[i]%2 == 0)
                nums[i] = 0;
            else
                nums[i] = 1;
        }

        unordered_map<int,int> mp;
        int pre=0, ans=0;
        mp[0]=1;

        for(int i=0; i<nums.size(); i++){
            pre += nums[i];

            if(mp.find(pre-k) != mp.end())
                ans += mp[pre-k];

            mp[pre]++;
        }

        return ans;
    }
};
```

{% endcode %}

### Approach 2:

```
Sliding Window
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int Atmost(vector<int>& nums, int k){
        int ans=0, odd=0, low=0;

        for(int high=0; high<nums.size(); high++){
            if(nums[high]%2 != 0)
                odd++;

            while(odd>k){
                if(nums[low]%2 != 0)
                    odd--;

                low++;
            }
            /* Here at each step we are not getting array lenght 
                But actually num of arrays
                1 2 1 (1
                1, 11, 211, 1211 like that 4
                
            */
            ans += high-low+1;
        }

        return ans;
    }

    int numberOfSubarrays(vector<int>& nums, int k) {
        return Atmost(nums, k) - Atmost(nums, k-1);
    }
};
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
