The algorithm is to use a partitioning algorithm from quickselect. We need to find the N-k smallest element in the array. We use a heap to sort an array and keep the top Kth largest element. The algorithm uses the fact that the work done keeps decreasing on each iteration, gives the O(n) runtime. The toughest thing in the algorithm is understanding how the partitioning works is to imagine an array in a sorted array in linear time using the Lomuto algorithm.