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 n, long long k, long long arr[]){vector<pair<long long, long long>> v;for (long long i = 0; i < n; i++)v.push_back({arr[i], i + 1});sort(v.begin(), v.end());long long cnt = 0;for (long long i = v.size() - 1; i >= 0; i--){cout << v[i].second << " ";cnt++;if (cnt == k)break;}}int main(){long long n = 5, k = 3;long long arr[n] = {25, 5, 17, 10, 45};topContestants(n, k, arr);return 0;}
No comments:
Post a Comment