Top k frequent number


not passes any test cases

Sample Input

1
5 2
5 1 3 5 2

Sample Output

5 1 5 1 3 5 1 5 1

But your code prints
3 5

In case you are not understanding the output format:

5 comes : print( { 5 } )
1 comes : print( { 1,5 } )
3 comes : print( { 1,3 } ) , all three have same frequency so taking smaller 2.
5 comes : print( { 5,1 } ) , now 5 has max frequency.
2 comes : print( { 5,1 } )

Final output : 5, 1 5, 1 3, 5 1, 5 1.

Help me with time complexity. Using Hashmap solves it in O(n*k) then why use heap.

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.