Sir, can you tell me what exactly has to be done?
As per my understanding, the question and the sample output are not matching.
Doubt in the question
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