Anirudh Sharma
BlogDSASkillsPortfolio
© Anirudh SharmaBlogDSASkillsGitHubEmail
← All problems

16 Nov 2026·Medium

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
Solve on LeetCode (#1524) ↗

On this page

  1. The problem
  2. The slow way
  3. The key idea
  4. See it step by step
  5. The algorithm
  6. Dry run
  7. Code
  8. Complexity
  9. Common mistakes
  10. Practice next

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

  1. A subarray sum is a difference of two prefix sums

    Let prefix[k] be the sum of the first k numbers, with prefix[0] = 0. The sum of arr[i..j-1] is prefix[j] - prefix[i]. So every subarray is just a pair of prefix sums.

  2. Only even or odd matters

    odd - even is odd and even - odd is odd. even - even and odd - odd are even. So a subarray has an odd sum exactly when its two prefix sums have different parity.

  3. 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, even and odd, and add the right one to the answer.

  4. Don't forget the empty prefix

    Before reading anything, the prefix sum is 0, which is even. Start with even = 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.

Element
start
1
3
5
2
Prefix sum
0
?
?
?
?
Parity
even
even prefixodd prefixcurrentpairs with current
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.

Step 0 of 4

The algorithm

  1. Set even = 1 (the empty prefix), odd = 0, prefix = 0, answer = 0.
  2. For each number x in the array, add it: prefix += x.
  3. If prefix is odd: answer += even, then odd += 1.
  4. Otherwise (prefix is even): answer += odd, then even += 1.
  5. Return answer % (10^9 + 7).

Dry run

arr = [1, 3, 5, 2]

ReadPrefix sumParityPairs withAddedevenoddanswer
start0even–0100
11odd1 earlier even+1111
34even1 earlier odd+1212
59odd2 earlier even+2224
211odd2 earlier even+2236

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 % MOD

Complexity

TimeO(n)
SpaceO(1)

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 int for the answer in Java or C++. For n = 10^5 the count can reach about 5 × 10^9, which overflows a 32-bit int. Use long / long long, then take the mod.
  • Writing prefix % 2 == 1 with negative numbers. In Java and C++, -3 % 2 is -1. This problem only has positive numbers, but % 2 != 0 is 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.

Practice next

  • 560. Subarray Sum Equals K ↗Same pairing trick, but count exact prefix sums in a hash map.
  • 974. Subarray Sums Divisible by K ↗Group prefix sums by remainder mod K instead of mod 2.
  • 1248. Count Number of Nice Subarrays ↗Count odd numbers with a prefix, then pair prefixes.
  • 525. Contiguous Array ↗Turn 0 into -1 and look for equal prefix sums.
← All problemsSubmit on LeetCode ↗