Student of the Year | Practice | GeeksforGeeks

Given a vector containing name of N student (in lowercase letters) and their marks in an exam. The task is to sort the students with respect to their marks (student with highest marks will be on top). If marks are same, consider lexicographic sorting for names.
Example 1: 
Input:
N = 4
michal 56
john 100
abbas 98
jordan 45
Output: 
john 100
abbas 98
michal 56
jordan 45
Explanation:
Marks of students in decreasing order is
as 100, 98, 56, 45. So, their names are
as john 100, abbas 98, michal 56, jordan 45.

Your Task:
Since this is a function problem, you don't need to take any input. You just need to complete the provided function sortMarks(). Output the name of students with their marks in decreasing order, each student in new line.
Constraints:
1 <= N <= 106
1 <= marks <= 106
0
hgaur7013 days ago
Using lambda function :
vector<pair<string, int>> sortMarks(vector<pair<string, int>> v, int N)
{
    sort(v.begin(), v.end(), [](pair<string, int> p1, pair<string, int> p2)
    {
      if(p1.second == p2.second) return (p1.first < p2.first);
             else return (p1.second > p2.second); });

return v;
}
+2
hermione2673 months ago
bool cmp(pair<string,int> a, pair<string,int > b){    if(a.second == b.second){       return a.first < b.first;   }   else   return a.second > b.second;}vector<pair<string, int>> sortMarks(vector<pair<string, int>> v, int N){     sort(v.begin(), v.end(), cmp);  return v;   }
0
karntiwari3 months ago
CPP:
bool mycomparision(pair<string,int>a,pair<string,int>b){   if(a.second>b.second)// comparision btw number if number is greater than return true   {       return true;   }   else if(a.second==b.second)// if number is same   {       if(a.first<b.first) // then comparision in string        {           return true;                  } // comparision btw string according to alphabatical order in which string is smaller than return true       else      {          return false;                }   }   return false;}vector<pair<string, int>> sortMarks(vector<pair<string, int>> v, int N){   sort(v.begin(),v.end(),mycomparision);// called comparision function   return v;      //Complete the code and return the sorted vector}
0
aakritiashi4 months ago
#include<bits/stdc++.h>#include<iostream>using namespace std;
bool sortbysec(const pair<string,int> &a,             const pair<string,int> &b){   return (a.second > b.second);}
int main(){   vector<pair<string,int>>v;      int n;   cout<<"enter no. of students: ";   cin>>n;      for(int i=0 ; i<n ; i++)   {       string s;       int x;       cin>>s>>x;       v.push_back({s,x});   }      for(int i=0; i<v.size(); i++)   {       cout<<v[i].first<<" "<<v[i].second<<endl;   }      cout<<"student of the year is "<<endl;      sort(v.begin(),v.end(),sortbysec);      for(int i=0; i<v.size(); i++)   {       cout<<v[i].first<<" "<<v[i].second<<endl;   }
} 
0
dreamvivek65 months ago
bool mycomp(pair<string,int> a,pair<string,int> b){    if(a.second>b.second) return true;  else if(a.second<b.second) return false;  else  {      if(a.first<b.first) return true;      else      return false;  }}vector<pair<string, int>> sortMarks(vector<pair<string, int>> v, int N){   //Complete the code and return the sorted vector sort(v.begin(),v.end(),mycomp);   return v;}
+2
badgujarsachin837 months ago
bool fun(pair<string,int> a1,pair<string,int> a2){
    if(a1.second>a2.second){
        return 1;
    }else if(a1.second==a2.second){
        if(a1.first<a2.first){
            return 1;
        }else{
            return 0;
        }
    }
    return 0;
}
vector<pair<string, int>> sortMarks(vector<pair<string, int>> v, int N){
        
    
    //Complete the code and return the sorted vector
    sort(v.begin(),v.end(),fun);
    return v;
}
0
noobcoderdj8 months ago
noobcoderdj
c++ two liner
vector<pair<string, int="">> sortMarks(vector<pair<string, int="">> v, int N){    sort(v.begin(), v.end(), [](pair<string, int=""> a, pair<string, int=""> b){        if(a.second == b.second)            return a.first<b.first; return="" a.second="">b.second;    });    return v;}
0
mscodi1 year ago
mscodi
bool cmp_by_sec(pair<string, int=""> p1, pair<string, int=""> p2){    if(p1.second > p2.second)    {        return 1;    }    else if(p1.second == p2.second)    {        if(p1.first < p2.first)        {            return 1;        }        else        {            return 0;        }    }     return 0;}vector<pair<string, int="">> sortMarks(vector<pair<string, int="">> v, int N){      sort(v.begin(), v.end(),cmp_by_sec);    return v;
0
fnxl
This comment was deleted.
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.20
bool myCmp(pair<string, int=""> p1, pair<string, int=""> p2){    if(p1.second > p2.second)    {        return 1;    }    else if(p1.second == p2.second)    {        if(p1.first < p2.first)        {            return 1;        }        else        {            return 0;        }    }        return 0;}vector<pair<string, int="">> sortMarks(vector<pair<string, int="">> v, int N){    sort(v.begin(), v.end(), myCmp);        return v;}
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.