Count minimum number of moves to front or end to sort an array | Function that counts the minimum moves required to covert arr [ ] to brr [ ] ; Base Case ; If arr [ i ] < arr [ j ] ; Include the current element ; Otherwise , excluding the current element ; Function that counts the minimum moves required to sort the array ; If both the arrays are equal ; No moves required ; Otherwise ; Print minimum operations required ; Driver code
function minOperations ( arr1 , arr2 , i , j , n ) { let f = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) f = 1 ; break ; } if ( f == 0 ) return 0 ; if ( i >= n j >= n ) return 0 ; if ( arr1 [ i ] < arr2 [ j ] ) return 1 + minOperations ( arr1 , arr2 , i + 1 , j + 1 , n ) ; return Math . max ( minOperations ( arr1 , arr2 , i , j + 1 , n ) , minOperations ( arr1 , arr2 , i + 1 , j , n ) ) ; } function minOperationsUtil ( arr , n ) { let brr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; brr . sort ( ) ; let f = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != brr [ i ] ) f = 1 ; break ; } if ( f == 1 ) document . write ( minOperations ( arr , brr , 0 , 0 , n ) ) ; else cout << " " ; } let arr = [ 4 , 7 , 2 , 3 , 9 ] ; let n = arr . length ; minOperationsUtil ( arr , n ) ;