 # COVIDTEST GETING ( WA) EVEN AFTER RIGHT ANSWER HELP ME I AM NEW HERE

You fixed that issue but now your basic test cases are failing. Like [1, 0, 1].
I see your fault. When you encounter the first ‘1’ you see another is 2 position ahead, so you update ans[c]=“NO”. But when you encounter the second ‘1’, it seems just fine, so you update ans[c] = “YES”.

I think you should break out of the main loop when you updated it to “NO” in the first place.

import java.util.*;

public class Covid19 {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int test,circle=0,i,p1=-1,p2=-1;

int t=test=sc.nextInt();

String [] ans=new String[test];

while(test!=0)

{

test--;

circle=sc.nextInt();

int[] entry=new int[circle];

for(i=0;i<circle;i++)

{

entry[i]=sc.nextInt();

if(entry[i]==1)

{

if(p1==-1)

{

p1=i;

}

else

{

p2=i;

}

if(p1!=-1 && p2!=-1)

{

if(p2-p1>=6)

{

ans[test]="YES";

p1=p2;

p2=-1;

}

else

{

ans[test]="NO";

}

}

else{

if(p1!=-1)

{

ans[test]="yes";

}

}

}

}

}

for(i=0;i<t;i++)

{

System.out.println(ans[i]);

}


}

}

hey bro look this one what is wrong in this one …???

I think you’re doing the same mistake again. Whenever you get a ( p2-p1 < 6 ), you should store the answer as “NO” and break out of the main loop and proceed to the next test case.
Since you’re continuing forward, you might get a ( p2-p1 >= 6 ), for which the answer will be updated to a “YES”, which is wrong. For example, [1,0,1,0,0,0,0,0,0,1]
The first case is bad (p1=0, p2=2). The second case is good (p1=2, p2=9). So you print “YES”.

Also, since you’re a beginner, I’d suggest you not to spend so much time on easy questions. You should look at the editorial for hints or the solution if you are stuck on a problem (easy ones) for hours. Otherwise, this will slow down your learning speed and productivity in competitive programming.

import java.util.*;

public class Covid19 {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int test,circle=0,i,p1=-1,p2=-1;

int t=test=sc.nextInt();


String ans=null;

while(test>=1&&test<=100)

{

test--;

circle=sc.nextInt();

int[] entry=new int[circle];

for(i=0;i<circle;i++)

{

entry[i]=sc.nextInt();

if(entry[i]==1)

{

if(p1==-1)

{

p1=i;

}

else

{

p2=i;

}

if(p1!=-1 && p2!=-1)

{

if(p2-p1>=6)

{

ans="YES";

p1=p2;

p2=-1;

}

else

{

ans="NO";

break;

}

}

else {if(p1!=-1 && p2==-1 )

{

ans="YES";

}

}

}

}

p1=-1;

p2=-1;

System.out.println(ans);

}

}


}

is it okay now bro…?

bro can i get your contact if you do not mind because i need someone to guide me …if you can please …i will not disturb you or not waste your time just i will seek help from you when i get stuck somewhere… so if you dont mind plz

@dilip_singh Can you write a description about what is your thought process/logic?

Input:

2
7
1 1 0 1 0 0 0
7
1 1 0 1 0 0 1


Your-Code Output:

YES
YES


NO
NO


There is a simple logic to solve the problem, keep track of the position of the \text{last person} in the line and when you encounter a new person, check whether the \text{new person index} -\text{last person index} >= 6, if it is then update your \text{last person index} otherwise break out of the loop and print the answer as NO.

Refer to the editorial here: COVIDLQ - Editorial.

There is still one mistake which can be fixed easily though. You’re taking the input as well as checking it for the conditions in the same loop. So when you get a “NO”, you break out of it and then you move on to the next test case. But the remaining input of this test case were still not taken from the input stream. For eg-

2
5
1 0 1 1 1
3
1 0 1


For first test case, you take input till third value (1) and then break. Then for second test case you variable circle will take the value ‘1’ whereas it was supposed to take ‘3’.

I’d advise you to take input in a different loop first and then do the computations in another loop.

I guess its better you post your doubts here. More people will be able to see it and you can get more insights. Also, I might not be available every time, but someone will definitely reply to your queries over here.

import java.util.*;

public class Covid19 {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int test,circle=0,i,p1=-1,p2=-1,flag=1;

int t=test=sc.nextInt();

while(test>=1&&test<=100)

{

test--;

circle=sc.nextInt();

int[] entry=new int[circle];

for(i=0;i<circle;i++)

{

entry[i]=sc.nextInt();

if(entry[i] ==1 && flag==1)

{

if(p1==-1)

{

p1=i;

}

else

{

p2=i;

}

if(p1!=-1 && p2!=-1 )

{

if(p2-p1<6)

{

flag=0;

}

else

{

p1=p2;

p2=-1;

flag=1;

}

}

}

}

p1=-1;

p2=-1;

if(flag==0)

{

System.out.println("NO");;

}

else

{

System.out.println("YES");

}

}

}


}

see i fixed the code still getting WA

no bro its giving me right answer see https://www.codechef.com/viewsolution/31977340

No its not, now your code is giving wrong answer for the second test-cases which is given in the problem i.e.

1
7
1 0 0 0 0 0 1


NO


YES


Read the problem statement carefully just by making your code work for a particular case doesn’t mean, your solution is correct that just a vague way saying your solution is correct.
Generate test-cases (5-10) on your own and check whether your program is giving the right answer or not.
I already told you the rough idea about how to build the solution.

NOTE: Write easy to understand code, your code is messy, at-least when you are asking in a forum, comment your code explaining about what you have done, choose self-describable variable names and then ask for help.
Why? Because no one has that much time to refactor your code and then start finding out bugs/mistakes and after the process finds out that there is a very non-sense bug in the code which you could have also found out.

If you are new to the forum read the following posts before making a post again:

hey bro still you are looking in my old code …i fixed all the issues in my previus code just look in that code …but still getting same WA error…

Listen I am looking at your current code only, try running your code on multiple test-cases from the command line directly on the test cases which I talked about earlier, your code is still giving the wrong answer because there is a very subtle bug in your program because of which you are getting WA, debug it you will find it.

3
7
1 1 0 1 0 0 0
7
1 1 0 1 0 0 1
7
1 0 0 0 0 0 1


NO
NO
NO


Correct Output:

NO
NO
YES


ya i got it bro… thank you brother … now its accepted … thank you a lot for your kind respond and help