Skip to content

Instantly share code, notes, and snippets.

@ambiso
Last active January 13, 2018 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambiso/6821a4d8b92c95f5ddeb20278af17bf2 to your computer and use it in GitHub Desktop.
Save ambiso/6821a4d8b92c95f5ddeb20278af17bf2 to your computer and use it in GitHub Desktop.
Count the number of even sum subarrays in an array
#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