> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count Number of Nice Subarrays Python Solution

> Tested Python solution for LeetCode 1248 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1248, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Sliding Window](/catalog/topics/sliding-window), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/count-number-of-nice-subarrays/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1248   # by problem number
lcpy gen -s count_number_of_nice_subarrays   # by problem name
```

## Problem

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

### Examples

```
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].
```

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

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_number_of_nice_subarrays/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_number_of_nice_subarrays/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(nums))
    # Space: O(len(nums))
    def number_of_subarrays(self, nums: list[int], k: int) -> int:
        prefix: dict[int, int] = {0: 1}
        odds = 0
        count = 0
        for num in nums:
            odds += num % 2
            count += prefix.get(odds - k, 0)
            prefix[odds] = prefix.get(odds, 0) + 1
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(len(nums)) | O(len(nums)) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.