please tell how to approach this using heap
How to solve using pq
hi @mahroosanwar0901_2642b14836027bbf,
Iterate through the array which contains stream of numbers.
To keep track of top k elements, make a top vector of size k+1.
For every element in the stream increase its frequency and store it in the last position of top vector. We can use hashing for efficiently fetching frequency of an element and increasing it.
Now find the position of element in top vector and iterate from that position to zero. For finding position we can make use of the find() function in C++ STL, it returns an iterator pointing to element if found in the vector.
And make that list of k+1 numbers sorted according to frequency and their value.
Print top k elements form top vector.
Repeat the above steps for every element in the stream.
we have not cover hashing till now please tell how to do it with prority queue
hey bro i even not found google and youtube plz tell how to do using pq
we have not cover hashing till now please tell how to do it with prority queue
hi @mahroosanwar0901_2642b14836027bbf, the same thing can be done using array as the frequency counter not necessarily maps. btw map is also same thing you can read about it it u want
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.