Number of Sub-arrays With Odd Sum

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 $10^9 + 7$.

30:00

Number of Sub-arrays With Odd Sum
medium
Free written solution
Topics
Companies

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+710^9 + 7.

Example 1:
Input: {"arr":[1,2,3,4,5,6,7]}
Output: 16
Constraints:
  • 1≤arr.length≤1051 \leq \text{arr.length} \leq 10^5

  • 1≤arr[i]≤1001 \leq \text{arr}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[1,2,3,4,5,6,7]}

P0

1

P1

2

P2

3

P3

4

P4

5

P5

6

P6

7

P7

● even prefix● odd prefix

even prefixes

1

odd prefixes

0

odd-sum subarrays

0

A subarray sum is the difference of two prefix sums, and it is odd only when one prefix is odd and the other even. Start with the empty prefix (sum 0), which is even: even = 1, odd = 0.

Variables
VariableValue
odd0
even1
count0
DepthFunction Call
Stack empty
0/15