The Number of Sub-arrays With Odd Sum problem asks you to count subarrays whose sum is odd. This coding problem uses the prefix sum parity trick: a subarray sum is odd if and only if the parities of its two endpoint prefix sums differ. The array, math, dynamic programming, and prefix sum interview pattern is the core.
Microsoft, Meta, Amazon, Google, and Bloomberg ask this because it reduces a subarray counting problem to prefix parity counting — a clean O(n) approach. Candidates who see "odd sum subarray" should immediately think "prefix sum parity" and then count pairs of prefix sums with different parity.
Prefix parity count. Track how many prefix sums so far have been even (even_count) and how many odd (odd_count). For each new prefix sum: if it's even, the number of subarrays ending here with odd sum = odd_count (pair with each previous odd prefix). If it's odd, it's even_count. Accumulate the answer. Apply mod 10^9+7.
arr=[1,3,5]. Prefix sums: 0,1,4,9.
Prefix sum parity problems are O(n) and based on the insight: odd - odd = even, even - even = even, odd - even = odd, even - odd = odd. So subarray sum is odd ⟺ prefix sums at endpoints have different parity. Count pairs of different-parity prefix sums incrementally as you scan. Practice this pattern for "count subarrays with sum divisible by k" — the same prefix-remainder hash map technique generalizes from parity (mod 2) to any modulus.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Stone Game II | Medium | Solve | |
| Sum of Absolute Differences in a Sorted Array | Medium | Solve | |
| Super Ugly Number | Medium | Solve | |
| Find Good Days to Rob the Bank | Medium | Solve | |
| Largest Sum of Averages | Medium | Solve |