My issue
I sorted even numbers and then odd numbers. Now, I started taking combinations of left half of sorted even/odd and right half of sorted even/odd. Why am I getting wrong answer. Can anyone please help!!
‘o’ represents odd array and ‘e’ represents even array in my code.
My code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define vii vector<vector<int>>
int main() {
int t;cin>>t;
while(t--){
vector<int> e,o;
int n; cin>>n;
for(int i=0;i<n;i++){
int x; cin>>x;
if(x%2!=0){
o.push_back(x);
}else{
e.push_back(x);
}
}
if(n%2!=0){
cout<<-1<<' '<<'\n';
}else if(o.size()%2!=0){
cout<<-1<<' '<<'\n';
}else{
sort(e.begin(),e.end());
sort(o.begin(),o.end());
int y = e.size();
for(int i=0;i<y/2;i++){
cout<<(e[y/2-i-1]+e[y-i-1])/2<<' ';
cout<<(e[y-i-1]-e[y/2-i-1])/2<<' ';
}
y = o.size();
for(int i=0;i<y/2;i++){
cout<<(o[y/2-i-1]+o[y-i-1])/2<<' ';
cout<<(o[y-i-1]-o[y/2-i-1])/2<<' ';
}
cout<<'\n';
}
}
return 0;
}
Problem Link: Alter Ego Practice Coding Problem - CodeChef