Input 1: 6
Input 2:{4, 9,5,3,2,10}
Output:{0, 0,1,3,4,0}
Explanation
The first student has no one head
Answers
Answer:
public class Main
{
public static void main(String[] args) {
int [] students = {4,9,5,3,2,10};
int counter;
for(int i=0; i<6; i++){
counter = 0;
for(int j=0; j<i; j++){
if (students[j] > students[i]){
counter += 1;
}
}
System.out.print(counter+" ");
}
}
}
Given input 1 as 6, which means there are six students in the list. And input 2 is the list of heights for each student, which is {4, 9, 5, 3, 2, 10}.
To find the number of students who are taller than each student in the list, except for the first student, we can use the following algorithm:
Initialize an empty list called result to store the output values.
Set the first element of the result list to 0, since the first student is assumed to be the shortest and has no one taller than them.
Loop through the list of heights, starting from the second student (index 1) up to the last student (index n-1).
For each student, compare their height with the heights of all the previous students in the list (students with indices lower than the current student).
Count the number of students who are taller than the current student and store the result in the corresponding index of the result list.
Return the result list as the output.
Using this algorithm, we can find that the output for the given input is {0, 0, 1, 3, 4, 0}. This means that the second student has no one taller than them, the third student has one student taller than them, the fourth student has three students taller than them, the fifth student has four students taller than them, and the last student has no one taller than them.
#SPJ3