why do the following code giving wrong answer

#include
using namespace std;
int main()
{
int Q,n,i;
long int a[n],b[n];
cin>>Q;
while(Q–)
{
for(i=0;i<n;i++)
cin>>a[i]>>b[i];
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
if(a[i]==b[i]-2||a[i]==b[i]+2||a[i]==b[i]+1)
cout<<“YES”<<endl;
else
cout<<“NO”<<endl;
}
else
{
if(a[i]==b[i]-2||a[i]==b[i]+2||a[i]==b[i]-1)
cout<<“YES”<<endl;
else
cout<<“NO”<<endl;
}
}
}
}

Hey @sandy_1643,

  • n is not initialized, Change n to q and remove the while loop
  • rest is fine,

Check your corrected code here.

Hope this helps!

1 Like

int Q,n,i;
long int a[n],b[n];

  1. You should get a runtime error here. N not being initialised, it can be any garbage value. If its $\le$0 or greater than {10}^{8} then you will get runtime error. Local IDes are usually very friendly, they initialise such values to 0 automatically. But codechef ide/judge does not do this.

  2. Please post the question link as well. People wont be able to help much if you just post your code without telling the question.