Minimum elements to be removed from the ends to make the array sorted | C # implementation of the approach ; Function to return the minimum number of elements to be removed from the ends of the array to make it sorted ; To store the readonly answer ; Two pointer loop ; While the array is increasing increment j ; Updating the ans ; Updating the left pointer ; Returning the readonly answer ; Driver code
using System ; class GFG { static int findMin ( int [ ] arr , int n ) { int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; ans = Math . Max ( ans , j - i ) ; i = j - 1 ; } return n - ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( findMin ( arr , n ) ) ; } }