Top contestants

You are given that N candidates participated in a contest. For each candidate, you are given A[i], representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Example:

Input:  n = 5, k=3, arr = [25,5,17,10,45]
Output: 5 1 3 

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void topContestants(long long nlong long klong long arr[])
{
    vector<pair<long longlong long>> v;
    for (long long i = 0i < ni++)
        v.push_back({arr[i], i + 1});
    sort(v.begin(), v.end());
    long long cnt = 0;
    for (long long i = v.size() - 1i >= 0i--)
    {
        cout << v[i].second << " ";
        cnt++;
        if (cnt == k)
            break;
    }
}
int main()
{
    long long n = 5k = 3;
    long long arr[n] = {255171045};

    topContestants(nkarr);

    return 0;
}


No comments:

Post a Comment