Geek and Contest || | Practice | GeeksforGeeks

Geek hosted a contest and N students participated in it. The score of each student is given by an integer array arr[]. The task is to print the number of each student (indexes) in the order they appear on the scoreboard.
A student with a maximum score appears first. If two people have the same score then a higher indexed student appears first.

Example 1:
Input:
N = 5
arr[] = {450, 230, 730, 230, 150}
Output:
3 1 4 2 5
Explanation:
1. The top scorer is at 3rd index, therefore 3 
at the first index.
2. The second-highest scorer is at 1st index therefore
 1 is at the second index.
3. The third-highest scorer is at 4th index therefore
 4 at the thirst index.



Example 2:
Input:
N = 3
arr[] = {500, 500, 500}
Output:
3 2 1


 
Your Task:
This is a full problem. You need to write the complete code.
1. The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains a single integer N.
3. The next line contains N space-separated integers.
 
Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(N).

Constraints:
2 ≤ N ≤ 105
1 ≤ cost[i] ≤ 104
0
snipperwolf3 months ago
from collections import defaultdictfor _ in range(int(input())):   n=int(input())   l1,l2=[],[]   a=[int(i) for i in input().split()]   dic=defaultdict(list)   for i in range(len(a)):       dic[a[i]].append(i+1)   l=list(dic.keys())   l.sort()   for i in l:       l1.append(list(dic[i]))   for i in l1:       for j in i:           l2.append(j)   for i in l2[::-1]:       print(i,end=" ")   print(" ")
0
deytulsi183 months ago
[C++] [Time Complexity: O(nlogn)] [Aux Space: O(n)]
 
/*
* author: deytulsi18
* problem: https://practice.geeksforgeeks.org/problems/geek-and-contest-1611828920/0/
* time complexity: O(nlogn)
* auxiliary space: O(n)
* language: cpp
*/
#include<bits/stdc++.h>
using namespace std;

struct Data {
 
    int a;
    int b;
 
    Data(int a, int b)
        : a(a), b(b)
    {
    }
};

struct cmp {
    bool operator()(Data const& p1, Data const& p2)
    {
        
        if (p1.b == p2.b)
            return p1.a < p2.a;
        return p1.b < p2.b;
        
    }
};

void solution()
{
    int n;
    cin >> n;
    
    priority_queue<Data, vector<Data>, cmp> pq;
    
    for (int i = 1; i <= n; i++)
    {
        int inp;
        cin >> inp;
        pq.push(Data(i, inp));
    }
        
    while (!pq.empty()) {
        Data p = pq.top();
        pq.pop();
        cout << p.a << " ";
    }
    
    cout << endl;
}

int main()
 {
	int testCases = 1;
	cin >> testCases;
	
	while (testCases-->0)
	    solution();
	return 0;
}
0
avinav26113 months ago
Easy C++ Solution
 
0
lokendrabaghel0987653 months ago
//JAVA SOLUTION
//LONG CODE BUT EASY TO UNDERSTAND
import java.util.*;import java.lang.*;import java.io.*;class GFG{public static void main (String[] args){    Scanner scan = new Scanner(System.in);    int T = scan.nextInt();    while(T-- > 0){       
        int N = scan.nextInt();            ArrayList<Integer> list = new ArrayList<>();        int[] answer = new int[N];        for(int i = 0; i < N; i++)        {            list.add(scan.nextInt());        }                for(int j = 0; j < list.size(); j++)        {            int tempIdx = 0;            int max = list.get(0);            for(int i = 0; i < list.size(); i++)               {                if(max <= list.get(i))                {                    tempIdx = i;                    max = list.get(i);                }            }            answer[j] = tempIdx+1;            list.set(tempIdx, -9999999);                    }        for(int i = 0; i < N; i++)        {            System.out.print(answer[i]+" ");        }        System.out.println();    }    }}
0
chessnoobdj4 months ago
Using vector 
#include<bits/stdc++.h>

int main(){
    int t;
    std::cin >> t;
    while(t--){
        int n;
        std::cin >> n;
        std::vector <std::pair<int, int>> vec(n);
        for(int i=0; i<n; i++){
            int tmp = 0;
            std::cin >> tmp;
            vec[i].first  = tmp;
            vec[i].second = i+1;
        }
        std::sort(vec.begin(), vec.end());
        std::reverse(vec.begin(), vec.end());
        for(int i=0; i<n; i++){
            std::cout << vec[i].second << " ";
        }
        std::cout << "\n";
    }
}
+2
badgujarsachin835 months ago
#include<iostream>
#include<queue>
using namespace std;
int main()
 {
	//code
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    priority_queue<pair<int,int>> pq;
	    int i=0;
	    while(n--){
	        int value;
	        cin>>value;
	        pq.push({value,i+1});
	        i++;
	    }
	    while(!pq.empty()){
	        cout<<pq.top().second<<" ";
	        pq.pop();
	    }
	    cout<<endl;
	}
	return 0;
}
0
momgupta0616 months ago
#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
cin>>t;
while(t--)
{
    int n;
cin>>n;

priority_queue<pair<int,int>>q;
for(int i=0;i<n;i++)
{int f;
cin>>f;
q.push({f,i+1});
}
while(!q.empty())
{
cout<<q.top().second<<" ";
q.pop();
}
cout<<"\n";
}//code
	
}
    


-1
Abhishek Gupta10 months ago
Abhishek Gupta
#include <bits stdc++.h="">using namespace std;
int main() {//code
int t;cin>>t;while(t--){
    int n;    cin>>n;
    int a[n+1];    for(int i=1;i<n+1;i++) {="" cin="">>a[i];
    }
    priority_queue<pair<int,int>> pq;
    for(int i=1;i<=n;i++)    {        pq.push(make_pair(a[i],i));    }
    while(!pq.empty())    {        pair<int,int> t = pq.top();
         cout<<t.second<<" ";="" pq.pop();="" }="" cout<<endl;="" }="" return="" 0;="" }="">
0
R Vishnu
This comment was deleted.
-1
Pawan chaudhary11 months ago
Pawan chaudhary
#include <bits stdc++.h="">using namespace std;
int main() {int t;cin>>t;while(t--){    int n;    cin>>n;    multimap<int,int>m;    for(int i=0;i<n;i++) {int="" x;="" cin="">>x;    m.insert({x,i+1});    }    vector<int>v;    for(auto i:m)    v.push_back(i.second);    reverse(v.begin(),v.end());    for(int i=0;i<n;i++) cout<<v[i]<<"="" ";="" cout<<endl;="" }="" return="" 0;="" }<b="">
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.