Given an array of integers arr
. Return the number of sub-arrays with odd sum.
As the answer may grow large, the answer must be computed modulo 10^9 + 7
.
Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays 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.
Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays 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.
Input: arr = [1,2,3,4,5,6,7] Output: 16
Input: arr = [100,100,99,99] Output: 4
Input: arr = [7] Output: 1
1 <= arr.length <= 10^5
1 <= arr[i] <= 100
# @param {Integer[]} arr
# @return {Integer}
def num_of_subarrays(arr)
sum = 0
odd = 0
even = 1
ret = 0
arr.each do |x|
sum += x
if sum.odd?
odd += 1
ret += even
else
even += 1
ret += odd
end
end
ret % 1_000_000_007
end
impl Solution {
pub fn num_of_subarrays(arr: Vec<i32>) -> i32 {
let mut sum = 0;
let mut odd = 0;
let mut even = 1;
let mut ret = 0;
for x in arr {
sum += x;
if sum % 2 == 1 {
odd += 1;
ret = (ret + even) % 1_000_000_007;
} else {
even += 1;
ret = (ret + odd) % 1_000_000_007;
}
}
ret
}
}