Question is not clear

I am able to get how we have to solve the problem.

@Debugger The idea is to store the top k elements with maximum frequency. To store them a vector or an array can be used. To keep the track of frequencies of elements create a HashMap to store element-frequency pair. Given a stream of numbers, when a new element appears in the stream update the frequency of that element in HashMap and put that element at the end of the list of K numbers (total k+1 elements) now compare adjacent elements of the list and swap if higher frequency element is stored next to it.

Algorithm:

Create a Hashmap hm, and an array of k + 1 length.
Traverse the input array from start to end.
Insert the element at k+1 th position of the array, update the frequency of that element in HashMap.
Now, traverse the temp array from start to end – 1
For very element, compare the frequency and swap if higher frequency element is stored next to it, if the frequency is same then swap is the next element is greater.
print the top k element in each traversal of original array.

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.