Not able to undertsand the logic for the solution even after reading editorial .
Top k most frequent numbers in a stream
You can do this question without using heap .
Refer this https://www.geeksforgeeks.org/find-top-k-or-most-frequent-numbers-in-a-stream/
For heap based approach, read this discussion Top k most frequent in a stream of integers