Write a method called kthSmallest that accepts a PriorityQueue of integers k and an integer as parameters

Question:

Write a method called kthSmallest that accepts a PriorityQueue of integers k and an integer as parameters and returns the kth-smallest integer from the priority queue. For example, if the queue passed stores the integers [42, 50, 45, 78, 61] and k is 4, return the fourth-smallest integer, which is 61. If k is 0 or negative or greater than the size of the queue, throw an IllegalArgumentException. If your method modifies the state of the queue during its computation, it should restore the queue before it returns. You may use one stack or queue as auxiliary storage.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: