Please explain the test cases for CARVANS

Problem link : CARVANS

I hope first test case is clear.

Test Case #2: 8 3 6

I would call cars with alphabets: A, B, C

A is in the front so it is moving at its maximum speed, B’s speed is 3 (lesser than A’s max speed) so it is moving at its max speed. C’s speed is 6 (greater than B’s max speed) but since B is moving ahead and C can’t overtake so C is not moving at its max speed. Hence A and B qualify for the answer => 2

Test Case #3: 4 5 1 2 3

Cars: A B C D E

Similarly, A is moving with its max speed (4), B can’t move at its max speed (5 which is greater than that of A) since A is in front of it so B has to reduce its speed. C can move at its max speed (1) and hence neither of D and E can move at their max speed, for their max speeds are greater than C (the car in front of them). Hence A and C qualify for the answer => 2

1 Like