Sliding window of minimum k set of elements - improved logic