Problem Link: MAKE_AB_SAME Problem - CodeChef
Help
Why is this giving wrong answer?
(Idea: I’m conting ones before and after for each index in array a, so if we encounter a case where a[i] == 0 && b[i] ==1 , then i’ll check if there’s any one before or after this index in the original array, if it’s so, then we can continue, else we can never make this index 1, also if a[i] == 1 && b[i] == 0 , then we can never make this 0 as ‘‘or’’ with 1 always gives 1.
// Contest: CodeChef - START82
// URL: https://www.codechef.com/problems/MAKE_AB_SAME
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ld> vld;
typedef vector<pair<ll , ll>> vpll;
typedef vector<pair<ld , ld>> vplld;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef priority_queue<ll> pq;
typedef priority_queue<pair<ll,ll>> pqp;
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define graph vector<vector<int>>
#define all(x) x.begin(), x.end()
const int MOD = (int)1e9 + 7;
ll pow_mod(ll x, ll n, ll m){ //(x^n MOD m)
ll ret = 1;
while(n){
if(n&1)
ret = ((ret*x)%m);
n >>= 1;
x = ((x*x)%m);
}
return ret;
}
void solve(){
int n;
cin>>n;
vi a(n), b(n);
for(auto &x : a)cin>>x;
for(auto &x : b)cin>>x;
vi ob(n,0), oa(n,0);
for(int i=1; i<n; ++i){
ob[i] = ob[i-1];
if(a[i-1] == 1)ob[i] += 1;
}
for(int i=n-2; i>=0; --i){
oa[i] = oa[i+1];
if(a[i+1] == 1)oa[i] += 1;
}
for(int i=0; i<n; ++i){
if(a[i] == 0 && b[i] == 1){
if(ob[i] == 0 && oa[i] == 0){
cout<<"NO";
return;
}
}
else if(a[i] ==1 && b[i] == 0){
cout<<"NO";
return;
}
}
cout<<"YES";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}```