Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinLength ( vector < int > & arr ) { int index = ( int ) arr . size ( ) - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } int main ( ) { vector < int > arr = { 7 , 8 , 5 , 0 , -1 , -1 , 0 , 1 , 2 , 3 , 4 } ; cout << findMinLength ( arr ) ; return 0 ; }