Whats wrong in the code
@malhotranaman83,
The error is that since Pair is inside the Main class. We will first create a Main class object and from that object refer to the Pair class.
Something like this should work:
Main y = new Main();
Pair z = y.new Pair(x, mp.get(x));
heap.add(z);
Also, make comparator static.
Also, I have explained the sample test case below:
you don’t have to fill the HashMap completely you have to process as the new number is read. then you have to print the top k numbers.
for this sample input:
1
5 2
5 1 3 5 2
first input 5:
freq 5->1
output: 5
second input 1:
freq 5->1
1->1
now both have the same you freq you have to print in inc order of their value.
output: 5 1 5
third input 3
freq 5->1
1->1
3->1
all have the same freq you have to print in inc order of their value.
output: 5 1 5 1 3
fourth input 5:
freq 5->2
1->1
3->1
top k element are 5 and 1
output: 5 1 5 1 3 5 1
fifth input 2:
freq 5->2
1->1
3->1
2->1
top k elements are 5 and 1
final output:5 1 5 1 3 5 1 5 1
I hope you understand it. if you have any doubt feel free to ask: 
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.