Help in CARVANS

I am not getting this problem statement “CARVANS” …Can you please explain the given EXAMPLE in the problem so that I can solve it.

From the 2nd tc-
3
8 3 6
Let us suppose that the cars are numbered from 1 to N. So, the 1st car with maximum speed 8 enters the road. Since,no other car is ahead of it, it can move freely. So the count becomes 1.
Now, the second car with max speed 3 enters. Since, the speed of 2nd car(speed=3) is less than 1st car(speed=8), it too can move. So count becomes 2.
Now 3rd car enters the road. Its speed is 6. Since, no car can overtake any car ahead of it, the 3rd car is forced to reduce its speed since, the car ahead of it has speed 3.
So the total number of cars moving at their maximum speed is 2.

Same for 3rd tc-
4 5 1 2 3
1st car can move freely, count=1
2nd car cannot
3rd car can move, count=2
4th car cannot
5th car cannot
So total count=2

3 Likes

Thankyou sir…I got it !!

Sir…why I am getting a wrong answer …I m comparing the previous car with the new car coming.

import java.util.*;
class abc
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=1;i<=t;i++)
{
int n=sc.nextInt();
int count=1;
int arr[]=new int[n];
for(int j=0;j<n;j++)
{
arr[j]=sc.nextInt();
}
for(int k=1;k<n;k++)
{
if(arr[k-1]>=arr[k])
count++;
}
System.out.println(count);
}
}
}

You should compare it with the minimum speeds of the previous cars.

1 Like