ALT problem : WA for 3rd test case even after printing -1

Im taking difference and addition of two consecutive terms and adding them to the two separate vectors after dividing by 2. With this approach code is producing the correct output for test cases given with example.
Test case:
1
2
23
Output:
-1

This is expected output and code is generating that as well. Still getting WA for this test case.

#include <bits/stdc++.h>
using namespace std;
#define f(a,b,n) for(int a=b; a<n; a++)
#define mod 1000000007

ll power(ll a, ll n){
    ll res = 1;
    while(n){
        if(n&1)
            res = (res*a);
        a = (a*a);
        n >>= 1;
    }
    return res;
}

int sub(int a, int b){
    return max(a,b)-min(a,b);
}


void solve(){
    int n; cin>>n;
    int b[n];
    f(i,0,n){
        cin>>b[i];
    }
    
    vector<int> f,s;
    for(int i=0; i<n-1; i+=2){
        int diff = sub(b[i],b[i+1]);
        int sum = b[i]+b[i+1];
        if(diff&1){
            cout<<-1<<endl;
            return;
        }
        f.push_back(sum/2);
        s.push_back(diff/2);
    }
    reverse(f.begin(), f.end());
    reverse(s.begin(), s.end());
    
    for(auto i : f){
        cout<<i<<" ";
    }
    for(auto i: s){
        cout<<i<<" ";
    }
    cout<<endl;
}


int main(){
    int cases;
    cin>>cases;
    while(cases--){
        solve();
    }
    return 0;
}



@sawy
your logic is not right.
plzz refer my c++ solution for better understanding of the logic
U have to separate odd and even numbers .

#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