forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1524.cpp
More file actions
22 lines (18 loc) · 644 Bytes
/
1524.cpp
File metadata and controls
22 lines (18 loc) · 644 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int numOfSubarrays(vector<int>& arr) {
int a= pow(10,9)+7;
int temp[2]= {1,0};
unsigned int res= 0;
int val= 0, n= arr.size();//temp for odd and even pair
for (int i=0;i<n;i++){
val= ((val+arr[i])%2 +2)%2;//compute
temp[val]= (temp[val]+1)%a;
}
res= (long long)(temp[0]%a)*(temp[1]%a)%a;
return res;
}
};
//========================================================
//Time complexity of the above algorithm(asymptotically): O(n), where n is the length of the input array
//Space complexity: O(1), constant space