This is my code and everything seems to run perfect yet 3 test cases show wrong ans

import java.util.*;
public class Main {

public static void main(String[] args) {
	
	Scanner scn=new Scanner(System.in);
	int t=scn.nextInt();
	
	while(t>0) {
		int n=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=scn.nextInt();
		}
		List<Integer> ans=pancakeSort(arr);
		
		for(int k:ans) {
			System.out.print(k +" ");
		}
		t--;
	}
}

public static int find(int[] arr,int n) {
	for(int i=0;i<arr.length;i++) {
		if(arr[i]==n) {
			return i;
		}
	}
	return -1;
}

public static void flip(int[] arr,int k) {
	for(int i=0;i<k/2;i++) {
		int temp=arr[i];
		arr[i]=arr[k-i-1];
		arr[k-i-1]=temp;
	}
}
public static List<Integer> pancakeSort(int[] arr) {
	
	List<Integer> list=new ArrayList<>();
	int n=arr.length;
	
	while(n>0) {
		int i=find(arr,n);
		if(i != n-1) {
			flip(arr,i+1);
			flip(arr,n);
			list.add(i+1);
			list.add(n);
		}
		n--;			
	}
	return list;

}

}

Can you tell exactly which test cases are failing?

test case 0, test case 6 and test case 7 are failing

import java.util.*;

import java.io.*;

public class Main {

    public static void main(String[] args) {

    

        Scanner scn=new Scanner(System.in);

        int t=scn.nextInt();

        

        while(t>0) {

            int n=scn.nextInt();

            int[] arr=new int[n];

            for(int i=0;i<n;i++) {

                arr[i]=scn.nextInt();

            }

            List<Integer> ans=pancakeSort(arr);

            

            for(int k:ans) {

                System.out.print(k +" ");

            }

            System.out.println();

            t--;

        }

    }

    public static int find(int[] arr,int n) {

        int min = -1,minindex = -1;

        for(int i=0;i<n;i++) {

            if(arr[i] > min) {

                min = arr[i];

                minindex = i;

            }

        }

        return minindex;

    }

    public static void flip(int[] arr,int k) {

        int temp, start = 0; 

        while (start < k) 

        { 

            temp = arr[start]; 

            arr[start] = arr[k]; 

            arr[k] = temp; 

            start++; 

            k--; 

        }

    }

    public static List<Integer> pancakeSort(int[] arr) {

        

        List<Integer> list=new ArrayList<>();

        int n=arr.length;

        

        while(n>1) {

            int i=find(arr,n);

            if(i != n-1) {

                flip(arr,i);

                list.add(i+1);

                flip(arr,n-1);                

                list.add(n);

            }

            n--;            

        }

        return list;

    }

}

Changes I have made in your code are as follows:-

  • Changed your flip function so that it finds out the largest element index in the subarray that is to be flipped and not the whole array (as we are trying to always shift the largest element to its correct position in each iteration)

  • Changed your constraint on n in the pancake sort function to n > 1 rather than n > 0 (as flipping an array of size 1 is not flipping at all)

  • Changed your call parameters for flip functions in pancake sort as they were incorrect

the test cases are failing still

okay I will check for further errors

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.