Top k most frequent no in a stream

bhaiya , we ahve to make two heaps minheap and maxheap?

Hello @CODER_JATIN actually no I don’t think you have to use two heaps.
Are you thinking of any particular approach?

how can i solve this question?

we are using one vector of size (k+1 ) to keep track of top k frequent numbers so far.
a map to store frequency.

when u read a new number number its frequency get updated right? that means its position might get change in top k frequent vector.
so there we are using logic similar to insertion sort i.e we are finding the best position of current element for insertion. and then we are inserting it there by perfoming swaps
and then simply printing thr resulting vector

and where are we using heap?

@tusharaggarwal272
sir atleast read what u r copying pasting

@CODER_JATIN with heap we are coming with this logic:
There are 2 ways by which you can solve this problem using heaps.

  1. Using a MAX-HEAP (with no size limit): Build a max heap based on frequency count of integers. At the end, the top k elements of your max heap will be the k most frequent in the stream.
  2. Using a MIN-Heap of size K (more efficient):Maintain a min heap of size k based on frequency count of integers.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.