Last active
January 13, 2018 21:51
-
-
Save ambiso/6821a4d8b92c95f5ddeb20278af17bf2 to your computer and use it in GitHub Desktop.
Count the number of even sum subarrays in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int even_pairs(const vector<bool> &v) { | |
int even_now = 0, odd_now = 0; | |
int total = 0; | |
for (bool x : v) { | |
int even_before = even_now; | |
int odd_before = odd_now; | |
if (!x) { | |
even_now = even_before+1; | |
odd_now = odd_before; | |
} else { | |
even_now = odd_before; | |
odd_now = even_before+1; | |
} | |
total += even_now; | |
} | |
return total; | |
} | |
int main(void) { | |
int m; | |
cin >> m; | |
while(m--) { | |
int n; | |
cin >> n; | |
vector<bool> vec; | |
vec.reserve(n); | |
while (n--) { | |
int num; | |
cin >> num; | |
vec.push_back(num); | |
} | |
cout << even_pairs(vec) << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment