Why is this code showing a TLE in just one case? What optimizations are possible?

import java.util.*;

public class Main {

public static void main(String args[]) {
    Scanner sc=new Scanner(System.in);
    int t=sc.nextInt();
    while(t-->0){
        PriorityQueue<Long> l=new PriorityQueue<>(Collections.reverseOrder());
    PriorityQueue<Long> r=new PriorityQueue<>();
    
        int n=sc.nextInt();
        for(int i=0;i<n;i++){
            long d=sc.nextLong();
            if(l.size()==0 && r.size()==0){
                l.add(d);
                System.out.print(d+" ");
                continue;
            }
            if(l.size()>r.size()){
                if(d<l.peek()){
                    r.add(l.poll());
                    l.add(d);
                }
                else{
                    r.add(d);
                }
            }
            else if(l.size()<r.size()){
                if(d>r.peek()){
                    l.add(r.poll());
                    r.add(d);
                }
                else{
                    l.add(d);
                }
            }
            else{

                if(d<l.peek()){
                    l.add(d);
                }
                else r.add(d);
            }
            if(l.size()==r.size()){
                System.out.print(((l.peek()+r.peek())/2)+" ");
            }
            else if(l.size()>r.size()){
                System.out.print(l.peek()+" ");
            }
            else{
                System.out.print(r.peek()+" ");
            }
        }
        System.out.println();
    }
}

}

hello @rishabh1000
use fast IO(search the same on internet).
I m not familiar with java so cant help u in that.

But that is not the solution to my problem. Is there any further optimation i can do to avoid the TLE (other than fast IO)?

no u approach is already optimised.

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.

It still shows TLE though.

i dont know much java, pls read about fast io in java and use it.

One optimisation could be taking minheapsize=0 , maxheapsize=0 and adjusting the size during operation it will reduce the time of calculating minheap.size() . Even I faced the same problem. But the code works perfectly fine in hackerblocks without TLE . Hope the technical person resolves the issue soon.

Still if you want to clear the question use
ios_base::sync_with_stdio(false);
cin.tie(NULL);
inside main()