Top k most frequent numbers in a stream of numbers

I am unable to pass the testcase for the above mentioned problem even though my code works well for the examples given. I am assuming that I am still missing out some major case which I can’t figure out. Please help!

Here is my code: https://ide.codingblocks.com/s/393048

hello @Yash5646

ur approach is not correct.

in ur freqency map u r storing elements frequecny all at once which is not correct.

if u r currenly at index i then u should only consider elements and their frequncy that are in this range [0…i] and decide ur answer based on this.

Hi @aman212yadav
The frequency array is only updating the frequency of elements that have been inputted i.e. till index i and then the heap is created. I don’t exactly undersatnd what I am doing wrong. Can you please be a bit more specific.

check now ->

your code was correct.

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.