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

# Number of Sub-arrays With Odd Sum

> Tested Python solution for LeetCode 1524 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 1524, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/number-of-subarrays-with-odd-sum/description/).

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

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

## Problem

\<p>Given an array of integers \<code>arr\</code>, return \<em>the number of subarrays with an \<strong>odd\</strong> sum\</em>.\</p>

\<p>Since the answer can be very large, return it modulo \<code>10\<sup>9\</sup> + 7\</code>.\</p>

### Examples

```
Input: arr = [1,3,5]
Output: 4
Explanation: All subarrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]]
All sub-arrays sum are [1,4,9,3,8,5].
Odd sums are [1,9,3,5] so the answer is 4.
```

```
Input: arr = [2,4,6]
Output: 0
Explanation: All subarrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]]
All sub-arrays sum are [2,6,12,4,10,6].
All sub-arrays have even sum and the answer is 0.
```

```
Input: arr = [1,2,3,4,5,6,7]
Output: 16
```

### Constraints

* 1 \<= arr.length \<= 10^5
* 1 \<= arr\[i] \<= 100

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def num_of_subarrays(self, arr: list[int]) -> int:
        mod = 1_000_000_007
        result = 0
        odd_prefixes = 0
        even_prefixes = 1  # empty prefix has even sum
        parity = 0
        for value in arr:
            parity ^= value & 1
            if parity:
                result += even_prefixes
                odd_prefixes += 1
            else:
                result += odd_prefixes
                even_prefixes += 1
        return result % mod
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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