For the contest - CodeWars 2.0 CWR22021 , in the question Chef and Rabbits, the code I have written passed all the test cases, but I am getting an NZEC error on submission. Here is the code.
Can someone please help me out…
int T = sc.nextInt();
while(T-->0){
int hops = 0;
int N = sc.nextInt();
int arr[] = new int[N];
for(int i=0;i<N;i++)
arr[i] = sc.nextInt();
int currPos = 0;
int largePos = 0;
int i=0;
int flag=0;
while(true && currPos <=N-1){
int p1=0,p2=0, j=0;
// currValue = arr[i];
p1 = i+1;
p2 = i+arr[i];
if(p2>=N-1){ // if I can reach the end of the array
hops++;
flag++;
break;
}
//finding the largest in the subarray -- beg
int large= -1;
for(j=p1;j<=p2;j++)
if(arr[j]>large){
large = arr[j];
largePos = j;
}
//finding the largest in the subarray -- end
if(large == 0)
break;
hops++;
i=largePos;
currPos = largePos;
}
if(flag==1||currPos>=N)
System.out.println(hops);
else
System.out.println(-1);
}