← Back to STL Containers
Question 491
Top K Largest
Top K Largest
Write a functionvector<int> topK(const vector<int>& v, int k)that returns the k largest values ofvin descending order (largest first), using apriority_queue<int>. You may assume 1 <= k <=v.size(). Duplicates count separately. The tests print the returned vector separated by spaces. For v = {5, 9, 2, 7, 1} and k = 3 the output is exactly what is shown in Expected Output. For v = {3, 3, 8} and k = 2 the output is: 8 3 Do NOT write a main function.
Expected Output:
9 7 5 8 3
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL ContainersQueues
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.