Explanation :
Step 1:- From first K numbers build a max heap so that the top element is largest in those K
numbers. Say that element is X.
Step 2:- Now as numbers are coming for processing, say coming number be A
If A>X then A cant be Kth smallest.
If A=X then no change is needed.
If A<X then X cant be Kth smallest element so replace X with A and again call heapify.
e.g. 5 6 1 8 3 2 4 and you have to find 3rd smallest element.
Now make max heap of 3 elements 5 6 1. 6 being the largest in these 3 will be on top of the
heap. Now as 8 will come 8>6 so we will neglect 8. When 3 will come since 3<6 we will replace
6 with 3 in the heap and again we will call heapify. After heapify 5 will be on the top. When 2
will come, 2<5 then 5 will be replaced by 2 and after heapification 3 will be on the top. When 4
will come since 4>3 then we will neglect the 4. Now at the end 3 is on the top of the heap so 3
is the3rd smallest element of the given numbers or array.
NOTE - For Kth largest build min heap.