Top k most frequent number in a

Hlo, I am not understanding why for input 2 (last element of array), we output only 1 , why not we output k elements from heap in this case

if possible provide some other test case with its output, it will be a great help

hi @akshatjain4545,
Sample Input

1 <- no of testcases
5 2 <- no of elements and value of k
5 1 3 5 2 <- running stream data

first we got 5
so top 2 most freqent nos are 5 only as we have only one element so we print only one
ans= 5

then we got 1
so top 2 most frequent nos are 1 and 5 (in increasing order)
ans= 5 1 5

then we got 3
so top 2 most frequent nos are 1 3 (increasing order)
ans= 5 1 5 1 3

then we got 5
so top 2 most frequent nos are 5 and 1(smallest one )
ans=5 1 5 1 3 5 1

ans so on

i hope you got the point
reference code --> https://ide.codingblocks.com/s/640329

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.

Hi @talhashamim001,
Thanks for the assistance, cleared my doubt

1 Like