The code below executes perfectly while compiling but fails when submitted. Can anyone tell me why?https://www.codechef.com/APRIL20B/problems/COVIDLQ

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef { 
      
   
    public static void main (String[] args) 
    { 
         try{
        Scanner sc= new Scanner(System.in);
        int[] A=new int[100];
        
        
       int T=sc.nextInt();
       
        int i,j,k;
        
        for(k=1;k<=T;k++)
        {
            int flag=0;
        int N=sc.nextInt();
        for(i=1;i<=N;i++)
        {
        A[i]=sc.nextInt();
       // System.out.print(a[i]+" ");
        }
        outerloop:
        for(i=1;i<=N;i++)
        {
            if(A[i]==1)
            {
                for(j=i+1;j<=i+5;j++)
                {
                 if(A[j]==1)
                 {
                    
                    // System.out.println("NO");
                     flag=1;
                     break outerloop;
                     
                 }
                }
            }
        }
        if(flag==1)
        {
            System.out.println("NO");
        }
        else
        {
        System.out.println("YES");
        }
        }
         }
        catch(Exception e)
        {
            return;
        }
          
    } 

}
1 Like
import java.util.;
import java.lang.;

This is not the correct way to import files.

import java.io.*;

Try this.

That’s a formatting issue, because the user did not format their code properly. Their actual code is correct, in that regard.

1 Like

i entered it properly, it got changed when i pasted it here somehow. So thats not the problem. Ignore first 3 lines

Try submitting this

1 Like

Problem is your first three lines.

Passing the sample != passing all (hidden) test cases.

I see two main issues with the code. First, A is too small, it has to be of size \geq 101 because you’re using 1-indexing. Second, which is probably a bigger problem, you have the line for(j=i+1;j<=i+5;j++). This is usually fine, but what if j exceeds N? The array may initially be filled with zeroes, but what if you have a large testcase followed by a smaller testcase? The array elements at positions > N will be filled with random garbage, which might also consist of ones. For example, try testing your code on this:

2
2
0 1
1
1

Also, please do format your code (read my comment above), otherwise, it’s hard/impossible for anyone else to test it.

2 Likes

Thank you, but i want to know whats wrong in my code.

ignore that, it got changed when i pasted it here

THANK YOU