Question

Java Programming Rash is known about his love for racing sports. He is an avid Formula...

Java Programming

Rash 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, Rash 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 will 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≤105
1≤speed≤109

SAMPLE INPUT

3
1
10
3
8 3 6
5
4 5 1 2 3

SAMPLE OUTPUT

1
2
2
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//i have tested this code in hackerearth and it passed all testcases

//do comment if any problem arises

//do thumbs up if it helps

//Code

import java.util.Scanner;

class TestClass {

public static void main(String args[]) throws Exception {

Scanner sc = new Scanner(System.in);

// number of test cases

int t;

// read number of test cass

t = sc.nextInt();

while (true) {

// size of maxSpeed

int size;

// read size of maxSpeed

size = sc.nextInt();

// array of maximum speeds of size number of cars

int[] maxSpeed = new int[size];

// read all elements of maxSpeed

for (int i = 0; i < size; i++)

maxSpeed[i] = sc.nextInt();

// maxCount is count of cars running at maximum speed

int maxCount = 0;

// current speed is minimum of speeds at which previous cars are running

int currentSpeed = maxSpeed[0];

// for all car in cars

for (int i = 0; i < size; i++) {

// if current minimum speed is greater than maximum speed of current vehicle

if (currentSpeed >= maxSpeed[i]) {

maxCount++;// increement maxCount

// set current minimum speed to current vehicle speed

currentSpeed = maxSpeed[i];

}

}

// print count of vehicles running at max speed

System.out.println(maxCount);

t--;

if (t == 0)

break;

}

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
Java Programming Rash is known about his love for racing sports. He is an avid Formula...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please code in java There are N employees already working for a company and M new...

    Please code in java There are N employees already working for a company and M new candidates eligible for the work. At the end of the financial year, each of the N employees demand for an increment. You are provided with the data of current_salary and the salary he/she expects. Also, there are M candidates we can recruit. We have the data of salary demanded by each eligible candidate. The company can spend maximum of X units of money. Now...

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • Java problem In this problem, the first input is a positive integer called n that will...

    Java problem In this problem, the first input is a positive integer called n that will represent the number of lines to process. The lines to be processed have one or more integers separated by whitespaces. For each of these lines, you must output: The minimum value of the integers The maximum value of the integers The sum of the integers It is worth to mention that the number of integers of each line is not known a priori and...

  • Given an array of integers representing heights of consecutive steps, we say that a tumble is...

    Given an array of integers representing heights of consecutive steps, we say that a tumble is a sequence of strictly decreasing numbers and the height of the tumble is the difference between the height of the top step and the bottom step. For example, if there are consecutive steps with heights 16, 9, 4 then they form a tumble of height 12 (= 16-4). With consecutive steps of heights, 0, 4, 12, 4, 5, 7, 2, 1 there is a...

  • C programming question please meet all of the following conditions don't just output the same answer ​​​​​​​ Given a...

    C programming question please meet all of the following conditions don't just output the same answer ​​​​​​​ Given a sequence a. The elements of a has the following infomations: index The index is followed by the input order, starts from 1. The input won't contain index , you have to record it yourself. Smaller input index has higher priority. admin level : Level starts from 0 to 999. level 0 has the highest priority, while level 999 has the lowest....

  • I want this using while loop This using stringin python Use list or some thing in...

    I want this using while loop This using stringin python Use list or some thing in python Using list in python I want answer as soon as posdible E. Last Number time limit per test: 1 second memory limit per test: 256 megabytes input standard input output standard output You are given a sequence of positive integers aj, , 03, ... Print the last element of the sequence. Input The input consists of multiple lines. The i-th line contains a...

  • C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an...

    C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an ad about the new cafe. The promotion is if the price of an item is N dollars, then you can buy the second item for half the price, the third item for a quarter of the original price, and so on, but if it becomes less than M dollars, then you have to pay M dollars. He wonders how much he has to pay...

  • Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing!...

    Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing! Problem Description The inner product (also known as the dot product or scalar product) of the elements of set A and the elements of set B of size is defined as the sum of the products of corresponding terms. For example, given set A as the array (2, 3, 4, 5, 6, 7, 8, 9; and set B as 6,5, 4, 3, 2, 7,...

  • Please write code in java You’re given a chess board with dimension n x n. There’s...

    Please write code in java You’re given a chess board with dimension n x n. There’s a king at the bottom right square of the board marked with s. The king needs to reach the top left square marked with e. The rest of the squares are labeled either with an integer p (marking a point) or with x marking an obstacle. Note that the king can move up, left and up-left (diagonal) only. Find the maximum points the king...

  • Iwant answer this using number. Airthemitics. Varibles by python This using if else in python The...

    Iwant answer this using number. Airthemitics. Varibles by python This using if else in python The third picture using if. Elif. Else This using for loop in python A. Perimeter of a Rectangle time limit per test 1 second memory limit per test: 256 megabytes input standard input output standard output You are given two positive integers a and b which are the sides of the rectangle. Print the perimeter of this rectangle. The formula for perimeter is P =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT