EOSUM - Editorial

PROBLEM:

Even Sum Array

DIFFICULTY:

Easy

PREREQUISITES:

None

PROBLEM:

Given an array of an integer A[N]A[N] where NN is the size of the array, you have to make the sum of the array even. for every A[i]A[i] (where 0 < = i < N) you can change the value of A[i]A[i] to even if it is odd and odd if it is even. An element can be changed only once. Print out maximum no. Of elements that can be changed.

QUICK EXPLANATION:

If the no. of even in the array is odd then the answer will be N - 1 else the answer will be N

EXPLANATION:

As we know that sum of any no. of even will always result in even. And the sum of two odd will always result in even. So we can always convert all the odd to even and we can convert pair of even no. to odd. So we just need to find whether we can pair every even no. or not. If we can then the answer will be N else the answer will be N - 1.

SOLUTION:

#include<bits.stdc++.h>
using namespace std;

int main() {
    int T;
    cin >> T;
    while (T--) {
        int N;
        cin >> N;
        int cnt = 0;
        for (int i = 0; i < N; i++) {
            int X;
            cin >> X;
            if (X % 2 == 0) cnt++;
        }
        if (cnt % 2 == 0)
            cout << N << endl;
        else
            cout << N - 1 << endl;
    }
}