my code is always giving Wrong answer on code chef even though it is correct and running well on my compiler.
link to the question : https://www.codechef.com/problems/RAINBOWA
link to my code : https://www.codechef.com/viewsolution/22015958
my code :
#include<iostream>
using namespace std;
int main()
{
int T,N,A[10000],sum1,sum2; // sum1 is sum of 1st half of the array sum2 is sum of 2nd half
int answer=1;
cin>>T;
sum1=0;sum2=0;
for(int i=0;i<T;i++)
{
cin>>N;
sum1=0;sum2=0;
for(int j=0;j<N;j++)
{
cin>>A[j];
if(N%2==0){if(j<=N/2-1){sum1=sum1+A[j];}else{sum2=sum2+A[j];}} // calculating sum1 and sum2 if N is even
if(N%2!=0){if(j<N/2){sum1=sum1+A[j];}else if(j>N/2){sum2=sum2+A[j];}} // if N is odd
if(j>0)
{
if(A[j-1]!=A[j]-1&&A[j-1]!=A[j]+1&&A[j-1]!=A[j]){answer = 0;}
}
}
if(answer==0||sum1!=sum2){cout<<"no"<<"\n";}else{cout<<"yes"<<"\n";}
}
return 0;
}
Comments
Post a Comment