Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | 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 ; Given arr [ ] ; Function call
function findMinLength ( arr ) { let index = parseInt ( arr . length ) - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } let arr = [ 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 ] ; let n = arr . length ; document . write ( findMinLength ( arr ) ) ;