Toppers Of Class | Practice | GeeksforGeeks

There is a class of N students and the task is to find the top K marks scorers. You need to 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.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.
Example 1:
Input:
N=5 K=3
arr[] = { 2, 2, 1, 3, 1 }
Output: 3 0 1 2 4
Explanation: Topper with 3 marks is present 
at 3rd index, 2 marks is present at 0th 
index and next at 1st index. 1 marks is present 
at 2 and 4.

Example 2:
Input:
N=4 K=1
arr[] = { 1, 2, 3, 4 } 
Output: 3
Explanation: The highest marks is at index 3.
Your Task:
Since this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function kTop() that takes array A and integer N as parameters and change the given array according to the given conditions. You do not have to return anything
 
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 106
1 ≤ M ≤ 106
0
nmnishant3 months ago
C++ [Only 3 lines solution]bool comp(node n1, node n2){   if(n1.marks != n2.marks) return n1.marks > n2.marks;   else return n1.index < n2.index;}
void kTop(node a[],int n){   sort(a, a+n, comp);} 
+1
amanshakya0073 months ago
// JAVA SIMPLE CODE
class pair implements Comparable<pair>{
        int marks;
        int index;
        pair(int marks,int index){
            this.marks = marks;
            this.index = index;
        }
        public int compareTo(pair o){
            if(this.marks == o.marks){
                return this.index - o.index;
            }
            else{
                return o.marks - this.marks;
            }
        }
    }
    void kTop(node a[],int n)
    {
        pair[] arr = new pair[n];
        for(int i=0;i<a.length;i++){
            node d = a[i];
            arr[i] = new pair(d.marks,d.index);
        }
        Arrays.sort(arr);
        for(int i=0;i<arr.length;i++){
            pair p = arr[i];
            node rr = new node();
            rr.setMarks(p.marks);
            rr.setIndex(p.index);
            a[i] = rr;
        }
        
    }
+1
badgujarsachin834 months ago
bool com (node a,node b){
    if(a.marks==b.marks){
        return a.index<b.index;
    }
    return a.marks>b.marks;
}
void kTop(node a[],int n)
{
    // Your code goes here
    sort(a,a+n,com);
}
+3
mayurmadhwani6 months ago
bool cmp(node a,node b){
    
    if(a.marks == b.marks)
        return a.index<b.index;
    
    return a.marks>b.marks;
    
}

void kTop(node a[],int n)
{
    
    sort(a,a+n,cmp);
    
}
+1
admiral_general_aladeen6 months ago
Fix the driver code in python.
a.sort(key=lambda x:(-x[0],x[1]))
This is simple solution but GFG never thinks of fixing the driver codes.
0
rahulsaidileep17 months ago
I think the default given code (python) is incorrect
0
mjohnmwangi7 months ago
 k is not available in the args for kTop
0
sujankumarmitra7 months ago
I think there's some error in the driver code.
 
 
Code:
class GFG
{
    
    static final Comparator<node> CMP = Comparator
                .comparing((node n)-> n.marks,Comparator.reverseOrder())
                .thenComparing(n -> n.index);
    
    void kTop(node a[],int n)
    {
        Arrays.sort(a, 0, n, CMP);
    }
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3306 out of bounds for length 3306 at Array.main(File.java:53)
0
noobcoderdj8 months ago
noobcoderdj
c++ one liner
void kTop(node a[],int n){    sort(a, a+n, [](struct node x, struct node y){        if(x.marks==y.marks)            return x.index<y.index; return="" x.marks="">y.marks;    });}
+1
Rohit negi11 months ago
Rohit negi
bool compare(node n1, node n2){    if(n1.marks>n2.marks)    return 1;    else if((n1.marks==n2.marks)&&(n1.index<n2.index)) return="" 1;="" return="" 0;="" }="" void="" ktop(node="" a[],int="" n)="" {="" your="" code="" goes="" here="" sort(a,a+n,compare);="" cout<<"1";="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.