← Back to STL Containers
Question 497
Kth Smallest with a Min-Heap
Kth Smallest with a Min-Heap
Write a functionint kthSmallest(const vector<int>& v, int k)that returns the k-th smallest value ofv, where k = 1 means the smallest of all. Use a min-heap,priority_queue<int, vector<int>, greater<int>>. You may assume 1 <= k <=v.size(). The tests print the returned value on its own line. For v = {7, 2, 9, 4, 5} and k = 2 the output is exactly what is shown in Expected Output. For v = {3, 1, 2} and k = 3 the output is: 3 Do NOT write a main function.
Expected Output:
4 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.