Top K Largest
mediumQuestion 491Write a function vector<int> topK(const vector<int>& v, int k) that returns the k largest values of v in descending order (largest first), using a priority_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, so 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.