**Carvans Codechef Solution:** Today we will be solvingÂ **Carvans CodeChef Solution**Â which is the part ofÂ **CodeChef **Solutions

## Problem

Most problems on CodeChef highlight chefâ€™s love for food and cooking but little is known about his love for racing sports. He is an avid Formula 1 fan. He went to watch this yearâ€™s Indian Grand Prix at New Delhi. He noticed that one segment of the circuit was a long straight road. It was impossible for a car to overtake other cars on this segment. Therefore, a car had to lower down its speed if there was a slower car in front of it. While watching the race, Chef started to wonder how many cars were moving at their maximum speed.

Formally, youâ€™re given the maximum speed of N cars in the order they entered the long straight segment of the circuit. Each car prefers to move at its maximum speed. If thatâ€™s not possible because of the front car being slow, it might have to lower its speed. It still moves at the fastest possible speed while avoiding any collisions. For the purpose of this problem, you can assume that the straight segment is infinitely long.

Count the number of cars which were moving at their maximum speed on the straight segment.

### Input

- The first line of the input contains a single integer T denoting the number of test cases to follow. Description of each test
- case contains 2 lines. The first of these lines contain a single integer N, the number of cars. The second line contains N space separated integers, denoting the maximum speed of the cars in the order they entered the long straight segment.

### Output

- For each test case, output a single line containing the number of cars which were moving at their maximum speed on the segment.

### Constraints

1 â‰¤ T â‰¤ 100

1 â‰¤ N â‰¤ 10,000

All speeds are distinct positive integers that fit in a 32 bit signed integer.

Each input file will not be larger than 4 MB (4,000,000,000 bytes) in size.

**WARNING!**Â The input files are very large. Use faster I/O.

### Example

Input:3 1 10 3 8 3 6 5 4 5 1 2 3Output:1 2 2

### Explanation

**Test caseÂ 1:**Â There is onlyÂ 1Â car. Thus, it can move at its maximum speed.

**Test caseÂ 22:**Â The first car moves at speedÂ 8. The second car can move at speedÂ 3Â without colliding with the first car. For the third car, if it moves at speedÂ 6, it would collide with the second car after some finite amount of time. The maximum speed at which the third car can move avoiding any collision isÂ 3. Thus,Â 2Â cars can move at their maximum speed.

**Test caseÂ 3:**Â Following are the maximum speeds of all the cars.

- The first car moves at a maximum speed ofÂ 4.
- The maximum speed at which the second car can move without colliding isÂ 4.
- The third car can move at its maximum speed which isÂ 1.
- The maximum speed at which the fourth car can move without colliding isÂ 1.
- The maximum speed at which the fifth car can move without colliding isÂ 1.

Thus, two cars can move at their maximum speed. These cars are carÂ 1Â andÂ 3.

## Carvans CodeChef Solution in JAVA

/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); int t= sc.nextInt(); while(t-->0) { int n= sc.nextInt(); int max= Integer.MAX_VALUE; int count=0; while(n-->0) { int speed=sc.nextInt(); if(max>=speed) { max=speed;count++; } } System.out.println(count); } } }

## Carvans CodeChef Solution in CPP

#include<iostream> using namespace std; int main(){ std::ios::sync_with_stdio(false); int t,n,s; cin>>t; while(t--){ int c=0,prev=0; cin>>n; for(int i=0;i<n;i++){ cin>>s; if(i==0){c++;prev=s;} else { if(s<=prev){c++;prev=s;} } } cout<<c<<endl; } }

** Disclaimer:Â The above Problem (**Carvans CodeChef

**) is generated by**Â CodeChef but the solution is provided byÂ

**Chase2learn**.This tutorial is only forÂ

**Educational**Â andÂ

**Learning**Â purpose.