I don't understand why my code is not able to pass all test cases as on leetcode same code is working. Pancake sorting problem

import java.util.*;

public class Main {

public static void main (String args[]) {

Scanner sc=new Scanner(System.in);

	if(sc.hasNext()){

		int t=sc.nextInt();

		while(t>0){
			int n=sc.nextInt();
			int [] arr=new int[n];
			for(int i=0;i<n;i++){
				arr[i]=sc.nextInt();
			}
			List<Integer> ans=pancakeSort(arr);
			for(int i:ans){
				System.out.print(i+" ");
			}
			t--;
		}
	}
}

public static List<Integer> pancakeSort(int[] a) {
    List<Integer> list = new ArrayList<>();

    int n=a.length;

	while(n>0) {

	int idx= find (a,n);

	if(idx!=n-1){
	flip(a,idx+1);

	flip(a,n);

	list.add(idx+1);

	list.add(n);
	}
	n--;
	}
    return list;
}

protected static void flip(int[] sublist, int k) {
    for(int i=0;i<k/2;i++){
		int t=sublist[i];
		sublist[i]=sublist[k-i-1];
		sublist[k-i-1]=t;
	}
}

protected static int find(int[] a, int target) {
    for (int i = 0; i < a.length; i++)
        if (a[i] == target)
            return i;
    return -1;
}

}