If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

It is also possible to just sort the input array in ascending order and pick the k-1th element.

dogbert82

That will take O(n) time. Idea here is to not sort the entire array. We can use priority queue or quick sort (i.e. partition only k times) and pivot will land in its correct position.

gau246

Why not just use Arrays.sort() and pick the k-1 th element?