Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | Java program for above approach ; Function to find the minimum length of prefix required to be deleted ; Stores index to be returned ; Iterate until previous element is <= current index ; Decrementing index ; Return index ; Driver code ; Given arr [ ] ; Function call
import java . util . * ; import java . io . * ; class GFG { static int findMinLength ( int [ ] arr ) { int index = ( int ) arr . length - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( findMinLength ( arr ) ) ; } }