Here is my code which is using a priority queue and I have defined a comparator to make it max priority queue
Java Code
package com.learning.dsa.Practice;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
public class ContestAns {
public static void main(String[] args){
Scanner scin = new Scanner(System.in);
int t = scin.nextInt();
while (t --> 0){
int n = scin.nextInt();
PriorityQueue<Integer> p = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer v1, Integer v2) {
if (v1 > v2){
return -1;
}
if (v1 < v2){
return 1;
}
else{
return 0;
}
}
});
for (int i = 0 ; i < n ; i++){
p.add(scin.nextInt());
}
p.forEach(System.out::println);
}
}
}
Input:
1
3
1 2 3
Output:
3
1
2
Why only in this test case the output is wrong? except for this test case, it is displaying the correct output