Ambiguity in question statement

Not able to understand the question statement clearly

@mudit2jain
Consider array as arr[] = {5, 2, 1, 3, 2} and k = 4

Step 1:After reading 5, there is only one element 5 whose frequency is max till now. so print 5.

Step 2:After reading 2, we will have two elements 2 and 5 with same frequency. As 2, is smaller than 5 but their frequency is same so we will print 2 5.

Step 3: After reading 1, we will have 3 elements 1,2 and 5 with same frequency, so print 1 2 5.

Step 4: Similarly after reading 3, print 1 2 3 5

Step 5: After reading last element 2, since 2 has already occurred so we have now frequency of 2 as 2. So we keep 2 at the top and then rest of element with same frequency in sorted order. So print, 2 1 3 5.

Final Output is
5 2 5 1 2 5 1 2 3 5 2 1 3 5

Can you explain the process if k = 2.
Thanks

Yes,
Consider
5 2
5 1 3 5 2

Ouput: 5 1 5 1 3 5 1 5 1

Rule:Consider when number of elements becomes k in our stream we will print only k elements else we will print complete array.

Step1: After reading 5 there is only one element 5 so print 5.
Console:5
Step2: After reading 1, there is only one element 1 so print 1 5 as both have same freq.
Console:5 1 5
Step3: After reading 3,there are three elements(and k=2) but with same freq,hence print 1 3 as k=2.
Console:5 1 5 1 3
Step4:After reading 5,we now have freq of 5 as 2,so print 5 first and then 1, as 5 1.
Console:5 1 5 1 3 5 1
Step 5:After reading 2, we still have freq of 5 as 2,so print 5 first then 1,as 5 1.
Console:5 1 5 1 3 5 1 5 1.

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.