# THIS CODE SHOWS WA ON LAST TEST!

``````#include <bits/stdc++.h>
using namespace std;
#define int long long

void solve() {
int n;
cin >> n;
int a[n], b[n];
for (int& i : a) cin >> i;
for (int& i : b) cin >> i;

bool can = true;
for (int i = 0; i < n - 3; i++) {
if (b[i] == a[i]) continue;
//simulate the operation
if ((b[i] - a[i]) == (a[i + 1] + a[i + 2])) {
a[i] += a[i + 1] + a[i + 2];
a[i + 3] += a[i + 1] + a[i + 2];
int t = a[i + 1];
a[i + 1] = -a[i + 2];
a[i + 2] = -t;
}
else {
can = false;
break;
}
}

for (int i = 0; i < n; i++) {
can &= a[i] == b[i];
}
cout << (can ? "yes" : "no") << "\n";
}

signed main() {
ios::sync_with_stdio(0);
cin.tie(0);

int t;
cin >> t;
while (t--) {
solve();
}
}
``````

Problem Link: Operating on A Practice Coding Problem - CodeChef

Please Help. Thank You.

Well, it should.

Your code assumes that the operation should be made from left to right, and that wasn’t stated in the problem. This is a counter example:

A = [8, 1, -8, 4, 7]
B = [1, 4, 3, 1, 3]

According to your code:
8 != 1
and
8 - 1 != 1 - 8

So, supposedly, it’s not possible.
But that’s not true.

I can perform the operation in index 2 (where it’s the num “1”)
So A becomes:
A = [8, -3, -4, 8, 3]

Then, perform the operation in index 1
A becomes:
A = [1, 4, 3, 1, 3], same as B.

There seems to be a correlation between the “apply this operation” problems and prefix sums. Specially, how does the prefix sums behaves when a given operation is done?

I wrote this:

``````#include <bits/stdc++.h>
#define ll long long
using namespace std;

void solve(){

ll N;
cin >> N;

vector<ll> A(N);
vector<ll> B(N);
for(ll i=0; i<N; i++){
cin >> A[i];
}
for(ll i=0; i<N; i++){
cin >> B[i];
}

vector<ll> preA(1, A[0]);
vector<ll> preB(1, B[0]);
for(ll i=1; i<N; i++){
preA.push_back(preA[i-1] + A[i]);
preB.push_back(preB[i-1] + B[i]);
}

map<ll,ll> oddA;
map<ll,ll> oddB;
map<ll,ll> evenA;
map<ll,ll> evenB;
for(ll i=0; i<N-1; i++){
if(i % 2 == 0){
evenA[preA[i]]++;
evenB[preB[i]]++;
}
else{
oddA[preA[i]]++;
oddB[preB[i]]++;
}
}

if ((evenA == evenB) && (oddA == oddB) && (preA[N-1] == preB[N-1])){
cout << "YES\n";
}
else{
cout << "NO\n";
}

}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;

cin >> T;
while(T--){
solve();
}
}

``````