text: Count minimum number of moves to front or end to sort an array | C ++ algorithm of above approach ; Driver Code ; Function to find minimum number of operation required so that array becomes meaningful ; Initializing vector of pair type which contains value and index of arr ; Sorting array num on the basis of value ; Initializing variables used to find maximum length of increasing streak in index ; Updating streak ; Returning number of elements left except streak
code: #include <bits/stdc++.h> NEW_LINE #include <vector> NEW_LINE using namespace std ; int main ( ) { int arr [ ] = { 4 , 7 , 2 , 3 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int count = minOperations ( arr , n ) ; cout << count ; } int minOperations ( int arr [ ] , int n ) { vector < pair < int , int > > vect ; for ( int i = 0 ; i < n ; i ++ ) { vect . push_back ( make_pair ( arr [ i ] , i ) ) ; } sort ( vect . begin ( ) , vect . end ( ) ) ; int res = 1 ; int streak = 1 ; int prev = vect [ 0 ] . second ; for ( int i = 1 ; i < n ; i ++ ) { if ( prev < vect [ i ] . second ) { res ++ ; streak = max ( streak , res ) ; } else res = 1 ; prev = vect [ i ] . second ; } return n - streak ; }