Doubt regarding COVID-19 question in may long challenge

My code is giving correct output ,but still it kept getting wrong in the codechef ide,I’m beginner,so can anyone please help me…

/* package codechef; // don’t place package name! */

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

/* Name of the class has to be “Main” only if the class is public. */
class codechef1
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=-1;
if(sc.hasNext())
t=sc.nextInt();
int j=0,i=0;
int arr[]=new int[10];
while(j<t)
{
int n= 1;
if(sc.hasNext())
n=sc.nextInt();
i=0;
int x[]=new int[n];

	           int xi;        
    		        
	                    while(i<n)
                	        {       if(sc.hasNext())
				x[i]=sc.nextInt();
                		       i++;
                	        }
	          

    		       
    		       
	          int c=1;
	          int k=0;
	            for(i=0;i<n-1;i++)
	            {     
	                if((x[i+1] - x[i]) <=2)
	                {
	                    c++;
	                    
	      	        }
		          else
		          { arr[k]=c;
		          k++;
		          c=1;
		          }
		          }
		          arr[k]=c;
		          
		          int val=arr[0];
		          int dc=0;
		          i=0;
                  while(arr[i]!=0)
                  {i++;}
                  k=i;
                    if(i==1)  
                      { System.out.println(arr[0]+ " " +arr[0]);
		                
                      }
		             else
		              { 
		                while(dc<i)
		                {
		                    if(arr[dc+1]>arr[dc])
		                    {val=arr[dc+1];
		                    }
		                    dc++;
		                }

                        System.out.println(arr[0] + " " + val);
		                  
		              }

                  
		      
		              
		          
            j++;
	                
      }
      
	           
	

}

}

Refer my solution
import java.io.;
import java.util.
;
import java.text.;
import java.math.
;
import java.util.regex.*;
class Solution {
public static void main(String[] args) {
int i,t,n,j;
Scanner sc = new Scanner(System.in);
t=sc.nextInt();
try{
while(t–>0)
{
n=sc.nextInt();
int a[]=new int[n];
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}int max=1,min=Integer.MAX_VALUE,c=1;
loop1: for(i=0;i<n;i++)
{c=1;
loop2: for(j=i;j<n-1;j++)
{
if(Math.abs(a[j]-a[j+1])<=2)
{c++;
i++;}
else
{
max=Math.max(max,c);
if(c>=1)
min=Math.min(min,c);
c=1;
continue loop1;
}
}
} max=Math.max(max,c);
min=Math.min(min,c);
if(max==n)
min=max;

 System.out.println(min+" "+max);
    }
    }
    catch(Exception e)
    {
        return;
    }
}

}

1 Like

your code fails at my input

1
10
565558 565568 565569 56555568 5556878 5555 5556 55568 55569 55571

right output :1 3
your output :1 4

man care about the constraints …

N=8 ,
0<=Xi<=10,

Try giving him numbers between 0 to 10.
For example check for these testcases if It fails then try to modify your logic.

5
5
0 3 6 9 10 Output : 1 2
7
0 1 4 5 8 9 10 Output: 2 3
6
0 1 4 5 8 9 Output : 2 2
5
0 1 4 7 10 Output : 1 1
8
0 1 2 3 4 5 6 7 Output : 8 8

Yes,i did compared from this and now its working.
Thank you

1 Like

Yes i have mentioned them this time,
Thanks for the help.

1 Like

Welcome bro