Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an array of integers arr, return the number of subarrays with an odd sum.
Since the answer can be very large, return it modulo 109 + 7.
Example 1:
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.
Example 2:
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.
Example 3:
Input: arr = [1,2,3,4,5,6,7] Output: 16
Constraints:
1 <= arr.length <= 1051 <= arr[i] <= 100Problem summary: Given an array of integers arr, return the number of subarrays with an odd sum. Since the answer can be very large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Dynamic Programming
[1,3,5]
[2,4,6]
[1,2,3,4,5,6,7]
subsequence-of-size-k-with-the-largest-even-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1524: Number of Sub-arrays With Odd Sum
class Solution {
public int numOfSubarrays(int[] arr) {
final int mod = (int) 1e9 + 7;
int[] cnt = {1, 0};
int ans = 0, s = 0;
for (int x : arr) {
s += x;
ans = (ans + cnt[s & 1 ^ 1]) % mod;
++cnt[s & 1];
}
return ans;
}
}
// Accepted solution for LeetCode #1524: Number of Sub-arrays With Odd Sum
func numOfSubarrays(arr []int) (ans int) {
const mod int = 1e9 + 7
cnt := [2]int{1, 0}
s := 0
for _, x := range arr {
s += x
ans = (ans + cnt[s&1^1]) % mod
cnt[s&1]++
}
return
}
# Accepted solution for LeetCode #1524: Number of Sub-arrays With Odd Sum
class Solution:
def numOfSubarrays(self, arr: List[int]) -> int:
mod = 10**9 + 7
cnt = [1, 0]
ans = s = 0
for x in arr:
s += x
ans = (ans + cnt[s & 1 ^ 1]) % mod
cnt[s & 1] += 1
return ans
// Accepted solution for LeetCode #1524: Number of Sub-arrays With Odd Sum
impl Solution {
pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
const MOD: i32 = 1_000_000_007;
let mut cnt = [1, 0];
let mut ans = 0;
let mut s = 0;
for &x in arr.iter() {
s += x;
ans = (ans + cnt[((s & 1) ^ 1) as usize]) % MOD;
cnt[(s & 1) as usize] += 1;
}
ans
}
}
// Accepted solution for LeetCode #1524: Number of Sub-arrays With Odd Sum
function numOfSubarrays(arr: number[]): number {
let ans = 0;
let s = 0;
const cnt: number[] = [1, 0];
const mod = 1e9 + 7;
for (const x of arr) {
s += x;
ans = (ans + cnt[(s & 1) ^ 1]) % mod;
cnt[s & 1]++;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.