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;
}