Help me in Problem MAXMINLEN

Scanner sc=new Scanner(System.in);
	int t=sc.nextInt();
	while(t-->0){
	    int n=sc.nextInt();
	    long a[]=new long[n];
	    for(int i=0;i<n;i++){
	        a[i]=sc.nextLong();
	    }
	    Arrays.sort(a);
	    long ans=0;
	    long max=Integer.MIN_VALUE;
	    long min=Integer.MAX_VALUE;
	    for(int i=n-1;i>0;i--){
	        max=a[i];
	        for(int j=i-1;j>=0;j--){
	            min=a[j];
	            long diff=max-min;
	            long inBwt=i-j-1;
	            long m=diff-2;
	            long nn=inBwt;
	            if(m==nn){
	                ans++;
	            }else if(nn>m){
                            // ways to select m element from nn element  
	                   long way=1;
	                    m=Math.min(m,(m-nn));
                        for(int r=0;r<m;r++){
                            way*=(nn-r);
                            way/=(r+1);
                        }
                        ans+=way;
	            } 
	            }
	        }
	        System.out.println(ans);
	    }

What is wrong in this doesn’t able to figure it out.
problem link:Max Min and Length Practice Coding Problem