CJCoding With Joseph
15free / daySign inCreate account

Top K Largest

Write 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 — print one after the last line.

Hints4 available
Topics:
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.