COLPROB-Editorial

PROBLEM LINK:

Practice
Author:shivamg4149
Editorialist:shivamg4149

DIFFICULTY:

Easy

EXPLANATION:

Pairwise difference and the pairwise sum will be even if all the boxes either contain an even number of chocolates or an odd number of chocolates.

So to make all even or odd we have to either add or remove chocolate in the box. We will smartly add or remove chocolates from the box by taking care of the count of total chocolates before and after the operation be minimum.

TIME COMPLEXITY:

O(N)

SOLUTIONS

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


int main(){
    int t;
    cin>>t;
    while(t--){
        long int n,odd=0,even=0,i;
        cin>>n;
        long int a[n];
        for(i=0;i<n;i++){
            cin>>a[i];
            if(a[i]%2==0)
                even++;
            else
                odd++;
        }

        if(even%2==0 && odd%2!=0)
            cout<<even<<endl;

        else if(even%2!=0 && odd%2==0)
            cout<<odd<<endl;

        else{
            if(even>odd)
                cout<<odd<<endl;

            else
                cout<<even<<endl;
        }
    }
}