Nzec error while submitting ?

Nzec error while submitting ? While it runs well in my pc ?

// My code is
import java.lang.*;
import java.util.Scanner;
class Temple
{
public static boolean check()
{
Scanner s = new Scanner(System.in);
int i=0,n=0;
int arr[]= new int[100];
n=s.nextInt();
for(i=0;i<n;i++)
arr[i]=s.nextInt();
if(n%2==0||n<3||n>100)
return false;
int h=n/2;
for(i=1;i<=h;i++)
{
if((arr[h-i]!=arr[h+i])||((arr[h-i]+1)!=(arr[h-i+1])))
return false;
}
return true;
}

// And its main. public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int s=0;
int i=0;
s=sc.nextInt();
for(i=0;i<s;i++)
{
if(check())
System.out.println(“Yes”);
else
System.out.println(“No”);
}
}
}

NZEC error is basically NON ZERO EXIT CODE.

In java, the prime reason for this is IndexOutOfBounds Exception, whether it may be string index or array…

Although NZEEC is given by any Exception thrown, IndexOutOfBounds Exception is prime reason for probably 95% NZEC submissions…

Hope this clarfies any doubt…

Please UPVOTE and Accept the answer if you find this helpful…

Hey,

You are initializing the array in a wrong way.

You should declare array as

int N= in.nextInt();

int arr[] = new int[N];

I hope this resolves the problem…

Please UPVOTE…

The problem TEMPLELA has a simpler solution.

When N is even, answer is invalid as there’s no possible heights satisfying all requirements…

When N is odd, observe ans for Odd values of N

N = 3: 1 2 1

N = 5: 1 2 3 2 1

N = 7: 1 2 3 4 3 2 1

A single loop in java

int N = in.nextInt();

int[] height = new int[N];

boolean ans = true;

for(int i = 0; i<N; i++)height[i] = in.nextInt();

for(int i = 0; i< N; i++){

   if(i < (N-1)/2){

           if(height[i] != i+1)ans = false;

   }else if(i == (N-1)/2){

           if(height[i] != (N+1)/2)ans = false;

   }else{

           if(height[i] != N - i)ans = false;

   }

}

if(ans && (N%2 != 0))System.out.println(“yes”);

else System.out.println(“no”);

PS: I know i shouldn’t post the whole solution, but it’s your first one, so cheers…

Please UPVOTE if you find this helpful…

My


[1]


  [1]: https://www.codechef.com/viewsolution/15437568
//