public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=1 ; i<=t ; i++)
{
// taking input
int n=sc.nextInt();
int [] arr=new int[n];
for(int j=0 ; j<n ; j++)
arr[j]=sc.nextInt();
int max=0 , minCount=0;
for(int j=0 ; j<n ; j++)
{
// checking that have we reached the end of array
if(j+arr[j]>=arr.length)
{
minCount++;
break;
}
// proceeding further
for(int k=j+1 ; k <= j+arr[j] ; k++)
{
// checking which value will take you more far
if(max<=k+arr[k] && arr[k]!=0)
{
max=k;
}
}
// checking if max has changed or not
if(max==0)
{
System.out.println(Integer.MAX_VALUE);
break;
}
// updating minCount and position of index i.e. (j)
minCount++;
j=max-1;
max=0;
}
System.out.println(minCount);
}
}
Whats wrong in this code
Hey @Himanshu-Jhawar-2273952536067590
you are doing wrong
try for this
int arr[] = { 1, 3, 6, 1, 0, 9 };
correct output : 3
debug your code :