can you please explain the question and output as i can not understand the how the output is occurring
Not understanding the question
Hello @shivamgoel150,
Sample Input
1
5 2
5 1 3 5 2
Sample Output
5 1 5 1 3 5 1 5 1
In the sample input first line is for the no. of testcases.
The second line has two elements the first element tell us the no. of total n elements and 2 is the k value that how many elements we need to print
And 3rd line is the given array 5 1 3 5 2
Now see the output part,
Now we need to output the nos. for every element of the array,
So if 5 is only there,
We have only one element and it’s frequency is 5 , so we will print 5
Now 5 1 is there in your array and the frequency of both the elements is 1 so we need to print the k elements and we have 5 with 1 frequency and 1 with 1 fequency and if the frequency is equal we will print the smallest element first of we print 1 5 for this part
And now suppose you have taken 3 as an input and till this case we have 3 elements in total and all have frequency 1 and we need to print the k elements only so we will print 1 3 starting from the smallest element.
So till know we have read 3 elements and the output is 5 1 5 1 3
And now again if we read 5 we have 5 with frequency 2 , 3 with frequency 1 and 1 with frequency 1.
So we will print 5 1 as 5 has higher frequency and we need to print the elements with higher frequency first
And now if we take 2 the output is still 5 1 as the frequency of 5 is higher and all the remaining elements have frequency of 1.
So the overall output is 5 1 5 1 3 5 1 5 1
I hope it is clear to you and in case there is any confusion is there pls let me know.
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.