Number of Sub-arrays With Odd Sum
Count the subarrays whose sum is odd, in one pass, by tracking whether each prefix sum is even or odd.
- Prefix Sum
- Parity & Modulo
- Counting Pairs
- Array
- Prefix Sum
- Math
- Counting
The problem
You get an array of integers arr. Count how many subarrays (contiguous, non-empty slices) have an odd sum. The count can be huge, so return it modulo 10^9 + 7.
Example 1
Input arr = [1, 3, 5]
Output 4
The subarrays are [1], [3], [5], [1,3], [3,5], [1,3,5] with sums 1, 3, 5, 4, 8, 9. Four of them are odd: 1, 3, 5 and 9.
Example 2
Input arr = [2, 4, 6]
Output 0
Every number is even, so every sum is even.
Example 3
Input arr = [1, 2, 3, 4, 5, 6, 7]
Output 16
Constraints: 1 <= arr.length <= 10^5 · 1 <= arr[i] <= 100
The slow way
Try every start i and every end j, keep a running sum, and count the odd ones. It is correct, but there are about n²/2 subarrays. With n = 100,000 that is 5 billion checks, far too slow.
Time O(n²)Space O(1)
The key idea
A subarray sum is a difference of two prefix sums
Let
prefix[k]be the sum of the firstknumbers, withprefix[0] = 0. The sum ofarr[i..j-1]isprefix[j] - prefix[i]. So every subarray is just a pair of prefix sums.Only even or odd matters
odd - evenis odd andeven - oddis odd.even - evenandodd - oddare even. So a subarray has an odd sum exactly when its two prefix sums have different parity.So just count, don't search
Walk left to right. At each step, if the current prefix sum is odd, it pairs with every earlier even prefix. If it is even, it pairs with every earlier odd prefix. Keep two counters,
evenandodd, and add the right one to the answer.Don't forget the empty prefix
Before reading anything, the prefix sum is
0, which is even. Start witheven = 1. That is what lets subarrays that start at index 0 be counted.
See it step by step
Press Next to read one number at a time. Green cells are even prefix sums, orange are odd. The outlined cells are the earlier prefixes with the opposite parity, and each one is an odd-sum subarray ending here. Type your own array to try it.
- Even seen before
- 1
- Odd seen before
- 0
- Added this step
- 0
- Answer so far
- 0
Before reading anything, the prefix sum is 0, which is even. That counts as one even prefix.
The algorithm
- Set
even = 1(the empty prefix),odd = 0,prefix = 0,answer = 0. - For each number
xin the array, add it:prefix += x. - If
prefixis odd:answer += even, thenodd += 1. - Otherwise (
prefixis even):answer += odd, theneven += 1. - Return
answer % (10^9 + 7).
Dry run
arr = [1, 3, 5, 2]
| Read | Prefix sum | Parity | Pairs with | Added | even | odd | answer |
|---|---|---|---|---|---|---|---|
| start | 0 | even | – | 0 | 1 | 0 | 0 |
| 1 | 1 | odd | 1 earlier even | +1 | 1 | 1 | 1 |
| 3 | 4 | even | 1 earlier odd | +1 | 2 | 1 | 2 |
| 5 | 9 | odd | 2 earlier even | +2 | 2 | 2 | 4 |
| 2 | 11 | odd | 2 earlier even | +2 | 2 | 3 | 6 |
The 6 odd subarrays are [1], [3], [5], [5, 2], [1, 3, 5] and [1, 3, 5, 2].
Code
Same logic in all three languages. The comments match the steps above.
class Solution:
def numOfSubarrays(self, arr: list[int]) -> int:
MOD = 10**9 + 7
# How many prefix sums we have seen, split by parity.
# The empty prefix (sum 0) is even, so even starts at 1.
even, odd = 1, 0
prefix = 0 # sum of the numbers read so far
answer = 0
for x in arr:
prefix += x
if prefix % 2 == 1:
# odd - even = odd: pair with every earlier even prefix
answer += even
odd += 1
else:
# even - odd = odd: pair with every earlier odd prefix
answer += odd
even += 1
return answer % MODComplexity
One pass over the array, and only four numbers are stored: even, odd, prefix and answer. No prefix array is needed.
Common mistakes
- Starting with
even = 0. You then miss every subarray that starts at index 0. The empty prefix counts as one even prefix. - Using
intfor the answer in Java or C++. Forn = 10^5the count can reach about5 × 10^9, which overflows a 32-bit int. Uselong/long long, then take the mod. - Writing
prefix % 2 == 1with negative numbers. In Java and C++,-3 % 2is-1. This problem only has positive numbers, but% 2 != 0is the safer habit. - Building every subarray. The O(n²) loop times out. You never need the subarrays themselves, only how many prefixes of each parity came before.