Challenge: Top Three
Completed
Read n (at least 3) and then n scores. Print the three highest scores, from highest to lowest, on one line separated by spaces.
Input
n, then n integers.
Output
Three integers.
Example
Input
6 55 90 72 88 61 95
Output
95 90 88
Editor
C++
Hints
Sort the vector from largest to smallest.
sort(v.begin(), v.end(), greater<int>());
Then the top three are at index 0, 1 and 2.
Solution explanation
After sorting in descending order the first three items are the highest scores.
Show solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> scores(n);
for (int& s : scores) {
cin >> s;
}
sort(scores.begin(), scores.end(), greater<int>());
cout << scores[0] << " " << scores[1] << " " << scores[2] << "\n";
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module