Ankit_132

C

Nov 22nd, 2023
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define pb     push_back
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         int n;
  14.         cin>>n;
  15.  
  16.         vector<int> a(n);
  17.         for(auto &e: a)     cin>>e;
  18.  
  19.         vector<int> b, c;
  20.  
  21.         for(auto e: a)
  22.         {
  23.             if(e%2)     b.pb(e);
  24.             else        c.pb(e);
  25.         }
  26.  
  27.         if(b.size()%2)
  28.         {
  29.             cout<<-1<<"\n";
  30.             continue;
  31.         }
  32.  
  33.         vector<int> ans(n);
  34.         int pos = 0;
  35.  
  36.         sort(b.rbegin(), b.rend());
  37.         sort(c.rbegin(), c.rend());
  38.  
  39.         for(int i=0; i<b.size()/2; i++)
  40.         {
  41.             int x = b[i] + b[b.size()-i-1];
  42.             x /= 2;
  43.             ans[pos] = x;
  44.             ans[pos+n/2] = b[i]-x;
  45.             pos++;
  46.         }
  47.  
  48.         for(int i=0; i<c.size()/2; i++)
  49.         {
  50.             int x = c[i] + c[c.size()-i-1];
  51.             x /= 2;
  52.             ans[pos] = x;
  53.             ans[pos+n/2] = c[i]-x;
  54.             pos++;
  55.         }
  56.  
  57.         for(auto e: ans)
  58.             cout<<e<<" ";
  59.         cout<<"\n";
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment