Algorithm
You are given a multiset (i. e. a set that can contain multiple equal integers) containing 2n integers. Determine if you can split it into exactly n pairs (i. e. each element should be in exactly one pair) so that the sum of the two elements in each pair is odd (i. e. when divided by 2, the remainder is 1).
The input consists of multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤100).
The second line of each test case contains 2n integers a1,a2,…,a2n (0≤ai≤100) — the numbers in the set.
For each test case, print "Yes" if it can be split into exactly n pairs so that the sum of the two elements in each pair is odd, and "No" otherwise. You can print each letter in any case.
5 2 2 3 4 5 3 2 3 4 5 5 5 1 2 4 1 2 3 4 1 5 3 2 6 7 3 4
Yes No No Yes No
In the first test case, a possible way of splitting the set is (2,3), (4,5).
In the second, third and fifth test case, we can prove that there isn't any possible way.
In the fourth test case, a possible way of splitting the set is (2,3).
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int od_sum = 0, ev_sum = 0;
int x;
for(int i = 0; i < 2*n; i++){
cin>>x;
if(x & 1){
od_sum++;
}
else{
ev_sum++;
}
}
if(od_sum == ev_sum){
cout<<"YES"<<endl;
continue;
}
cout<<"NO"<<endl;
}
}
Copy The Code &
Try With Live Editor
Input
2
2 3 4 5
3
2 3 4 5 5 5
1
2 4
1
2 3
4
1 5 3 2 6 7 3 4
Output
No
No
Yes
No
Demonstration
Codeforcess Solution 1542-A A. Odd Set ,C++, Java, Js and Python,1542-A,Codeforcess Solution