Help me in solving ALT problem

My issue

Array B consist of N pair.say pair(sum,diff)
If Array A possible .sum=x+y,diff=|x-y|where x and y belongs to A.if sum even->diff even.if sum odd ->diff(odd).create two arr odd and even if both are of even size then Array A possible.sort the both arr in descending order such that sum belong to left half and diff belong to right half.

For test case 3 5 4 4 it is printed 4 4 1 1.but correct o/p is 4 4 0 1

My code

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

int main() {
	  int t;cin>>t;
	  while(t--){
	      int n;cin>>n;
	      vector<int>e,o,a(n);
	      
	      
	      for(int i=0;i<n;i++){
	         int x;
	         cin>>x;
	         x&1?o.push_back(x):e.push_back(x);
	      }
	      if(o.size()%2>0||e.size()%2>0){
	          cout<<-1<<endl;
	          continue;
	      }
	     
	      sort(e.begin(),e.end(),greater<int>());
	      sort(o.begin(),o.end(),greater<int>());
	     
	      int eSize=e.size(),oSize=o.size();
	      int j=0;
	      for(int i=0;i<eSize/2;i++){
	          int x=(e[i]+e[i+(eSize/2)])/2;
	          int y=(e[i]-e[i+(eSize/2)])/2;
	          a[j]=x;
	          a[j+n/2]=y;
	          j++;
	      }
	      
	      for(int i=0;i<oSize;i++){
	          int x=(o[i]+o[i+(oSize/2)])/2;
	          int y=(o[i]-o[i+(oSize/2)])/2;
	          a[j]=x;
	          a[j+n/2]=y;
	          j++;
	      }
	      for(auto u:a)
	      cout<<u<<" ";
	      
	      cout<<endl;
	  }
	
	  return 0;
}

Problem Link: Alter Ego Practice Coding Problem - CodeChef

@nadiyasoaib
i think u have made some mistake in implementation part
here refer my c++ code to debug it.
but your logic seems correct .

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<algorithm>
#include<numeric>

using namespace std;
typedef long long int lli;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//typedef long long int lli;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> multi_pbds;
//typedef tree<pair<int , int>, null_type, less<pair<int ,int>>, rb_tree_tag, tree_order_statistics_node_update> pbdsp;
//typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define nl "\n";
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define db long double
#define prq priority_queue<lli>
#define psq priority_queue<lli,vector<lli>,greater<lli>>
#define mod 1000000007
#define lb lower_bound
#define ub upper_bound
#define vlli vector<lli>
#define mslli multiset<lli>
#define inf 1e17
#define sp " "
#define pb push_back
#define pie 3.14159265358979323846
#define test lli t; cin>>t; while(t--)
int32_t main()
{
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    test{
        lli n;
        cin>>n;
        lli a[n];
        vector<lli> o,e;
        lli cnt=0;
        for(lli i=0;i<n;i++)
        {
            cin>>a[i];
            if(a[i]%2)
            cnt++;
            if(a[i]%2)
            o.push_back(a[i]);
            else
            e.push_back(a[i]);
        }
        if(cnt%2)
        cout<<-1;
        else
        {
        sort(e.begin(),e.end());
        sort(o.begin(),o.end());
        int k=0;
        lli ans[n];
        for(int i=0,j=o.size()-1;i<j;i++,j--)
        {
            lli val=(o[i]+o[j])/2;
            ans[k]=val;
            ans[k+(n/2)]=val-o[i];
            k++;
        }
        for(int i=0,j=e.size()-1;i<j;i++,j--)
        {
            lli val=(e[i]+e[j])/2;
            ans[k]=val;
            ans[k+(n/2)]=val-e[i];
            k++;
        }
            for(int i=0;i<n;i++)
                cout<<ans[i]<<" ";
        }
        cout<<endl;
    }

}
1 Like