Toppers of Class
There is a class of N students and the task is to find the top K marks-scorers. Write a program that will print the index of the toppers of the class which will be same as the index of the student in the input array (use 0-based indexing). First print the index of the students having highest marks then the students with second highest and so on. If there are more than one students having same marks then print their indices in ascending order.
Input Example:
Suppose k = 2 and the students having highest marks have indices 0 and 5 and students having second highest marks have indices 6 and 7 then output will be 0 5 6 7.
K=2
N=10
Marks are:
97 84 82 89 84 97 95 95 84 86
Output:
0 5 6 7, so there are four toppers for K=2
97 & 95 is to be considered. There are four students who has got this. Their indices are 0 5 6 7 (For a particular marks if there is more than one student, their indices needs to printed in ascending order).
Data structure used:
Algorithm:
Implementation with the data structures used:
Here our key is integer type which maps to a vector of integer. Clearly the key is marks & which maps to a list of indices of students.
Here the elements are the marks which are stored in sorted descending fashion.
Let's consider the above input example:
So after completion of input taking:
Records looks like:
Numbers looks like:
K=2, thus we need to print indices only for marks 97, 95
Thus the indices to be print are: 0 5 6 7
So to print:
C++ implementation for Toppers of Class
Output
need an explanation for this answer? contact us directly to get an explanation for this answer