Need help in this code

Can you please tell me why my code didnt work for this problem ? Thank you
here is the question link ALT Problem - CodeChef

my code include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {

int t;
cin>>t;
while(t--)
{
    ll n;
    cin>>n;
    
    vector<ll> A(n);
    
    for(int i=0; i<n; i++)
    {
        cin>>A[i];
    }
    
    
    sort(A.begin(),A.end());
   
    vector<int> v;
    
    for(int i=0; i<n/2; i++)
    {
        ll sum =A[i]+A[n-1-i];
        ll diff=abs(A[i]-A[n-1-i]);
        
        v.push_back(sum/2);
        v.push_back(diff/2);
        
    }
    
    ll sum=0;
    
    for(int i=n/2; i<n; i++)
    {
        sum+=A[i];
    }
    
    ll tum=0;
    for(auto it:v)
    {
        tum+=it;
    }
    
    if(sum==tum)
    {
        
    for(auto it:v)
    cout<<it<<" ";
    cout<<endl;
    
    }
    else
    cout<<-1<<endl;
    
}

return 0;

}

the test case it showed me wrong result upon

@sar07
your logic is not right
plzz refer my c++ code for better understanding of the logic

#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

@sar07
You have to see what is asked in the question…The question demands for the largest SUM possible of the array A so check on that and there are a few logic mistakes …best thing you can do is to recheck your logic on paper with all the demands in mind

Or maybe check the previous submission and their explanation for better understanding
Good luck

1 Like

Thank you for your help

Thank you deepanshu for your help.