CARVANS WRONG ANSWER

why my answer is wrong.On my computer it shows correct answer.Where my code is wrong ?
import java.util.Scanner;

class CarRace {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t = s.nextInt();
int car =1,MAX=0;
while(t–>0)
{
int n = s.nextInt();
MAX = s.nextInt();
for(int i=1; i<n; i++)
{
int car1 = s.nextInt();
if(car1<=MAX)
{
car++;
MAX = car1;
}
}
System.out.println(car);
}

}

}

If you can please mention the problem link and format your code a bit? :slight_smile:


import java.util.Scanner;

class CarRace {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t = s.nextInt();
int car =1,MAX=0;
while(t–>0)
{
int n = s.nextInt();
MAX = s.nextInt();
for(int i=1; i<n; i++)
{
int car1 = s.nextInt();
if(car1<=MAX)
{
car++;
MAX = car1;
}
}
System.out.println(car);
}

}

}

You forgot to initialize the variable ‘car’ to 1 again in the while loop

Here’s your AC code


import java.util.*;
public class Car {
   public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
			int t = s.nextInt();
			int car =1,MAX=0;
			while(t-->0)
				{
				  int n = s.nextInt();
				  MAX = s.nextInt();
				  car=1;
				  for(int i=1; i < n; i++)
				  {
				    int car1 = s.nextInt();
				    if(car1<=MAX)
						{
						 car++;
						 MAX = car1;
						}
					}
				 System.out.println(car);
				}
 
	}
 
} 


P.S- Instead of scanner,use faster methods of IO in java

Thanks for your help.