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

int test,ncircle;

Scanner sc=new Scanner(System.in);

test=sc.nextInt();

String[] ans=new String[test];

int c=0;

while(test!=0)

{

test--;

ncircle=sc.nextInt();

int[] series=new int[ncircle];

for(int i=0;i<ncircle;i++)

{

series[i]=sc.nextInt();

}

for(int i=0;i<series.length;i++)

{

if(series[i]==1)

{

if(i+5<series.length)

{

for(int j=i+1;j<=i+5;j++)

{

if(series[j]==1)

{

ans[c]="NO";

break;

}

else

{

ans[c]="YES";

}

}

}

else

{

for(int j=i+1;j<series.length;j++)

{

if(series[j]==1)

{

ans[c]="NO";

break;

}

else

ans[c]="YES";

}

}

}

}

c++;

}

for(int ii=0;ii<ans.length;ii++)

{

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

}

One of the mistakes I could see is that the inner loops with iterator ‘j’ will never work if your series is of length 1 only. And then ans[c] will probably contain null value.

Also, that loop won’t work if your input contains only zeroes or exactly one 1 at the very end.
Try these for example :
[0,0,0,0,0] and [0,0,0,0,1]

YES BRO I FIXED THAT TOO BUT STILL SAME RESULT

BUT ITS GIVEN THAT QUE CAN NOT BE COMPLETLY EMPTY HENCE 0,0,0,0,0 CAN NOT BE THE CASE

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");

}

}

}


}