forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberOfSubArraysWithOddSum.cpp
71 lines (67 loc) · 1.89 KB
/
NumberOfSubArraysWithOddSum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Source : https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/
// Author : Hao Chen
// Date : 2020-10-07
/*****************************************************************************************************
*
* 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.
*
* Example 1:
*
* 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.
*
* Example 2:
*
* 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.
*
* Example 3:
*
* Input: arr = [1,2,3,4,5,6,7]
* Output: 16
*
* Example 4:
*
* Input: arr = [100,100,99,99]
* Output: 4
*
* Example 5:
*
* Input: arr = [7]
* Output: 1
*
* Constraints:
*
* 1 <= arr.length <= 10^5
* 1 <= arr[i] <= 100
******************************************************************************************************/
class Solution {
public:
int numOfSubarrays(vector<int>& arr) {
//Walk through the array, and calculate the current sum
//if current sum is odd, then all of the sum are evil previously are valid sub-array
//if current sum is evil, then all of the sum are odd previously are valid sub-array
int odd_sum_cnt=0, evil_sum_cnt=0;
int sum = 0;
long long result=0;
for (auto a : arr) {
sum += a;
if (sum % 2 == 0) {
evil_sum_cnt++;
result += odd_sum_cnt;
}else {
odd_sum_cnt++;
result += evil_sum_cnt + 1;
}
}
return result % 1000000007;
}
};