Help for MAKE_AB_SAME problem

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

@sudharshan281
your logic is correct i have done the same thing but have reduced like for answer to be yes a[0]must be equal to b[0] and a[n-1] must be equal to b[n-1] else it would be no
and the above condition met then if a[i]!=b[i] and a[i]==1 then it would be NO or else if(a[i]!=b[i] and a[i]==0 then if a[i] contains atleast one 1 then it would be yes else NO.
this is my code i hope u will get it.

include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n],b[n];
int fst=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==1)
{
fst=1;
}
}
for(int i=0;i<n;i++)
{
cin>>b[i];
}
if(a[0]==b[0]&&a[n-1]==b[n-1])
{
int ch=0;
for(int i=1;i<n-1;i++)
{
if(a[i]!=b[i]&&a[i]==1)
{
ch=1;
break;
}
else if(a[i]!=b[i]&&a[i]==0)
{
if(!fst)
{
ch=1;
break;
}
}
}
if(ch)
cout<<“NO”;
else
cout<<“YES”;
}
else
cout<<“NO”;
cout<<endl;
}
return 0;
}

Thanks for the reply, if you can give a test case where my code fails, that would be really helpful.

@sudharshan281
1
5
1 0 0 0 0
1 0 0 0 1
the answer would be NO it is giving yes because of end points condition that u haven’t mentioned in your code

1 Like

Thanks for taking time to help me out. Thank you