Minimum elements to be removed from the ends to make the array sorted

Given an array arr[] of length N, the task is to remove the minimum number of elements from the ends of the array to make the array non-decreasing. Elements can only be removed from the left or the right end.

Examples:

Input: arr[] = {1, 2, 4, 1, 5}
Output: 2
We can’t make the array sorted after one removal.
But if we remove 2 elements from the right end, the
array becomes {1, 2, 4} which is sorted.

Input: arr[] = {3, 2, 1}
Output: 2

Approach: A very simple solution to this problem is to find the length of the longest non-decreasing subarray of the given array. Let’s say the length is L. So, the count of elements that need to be removed will be N – L. The length of the longest non-decreasing subarray can be easily found using the approach discussed in this article.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the minimum number
// of elements to be removed from the ends
// of the array to make it sorted
int findMin(int* arr, int n)
{
  
    // To store the final answer
    int ans = 1;
  
    // Two pointer loop
    for (int i = 0; i < n; i++) {
        int j = i + 1;
  
        // While the array is increasing increment j
        while (j < n and arr[j] >= arr[j - 1])
            j++;
  
        // Updating the ans
        ans = max(ans, j - i);
  
        // Updating the left pointer
        i = j - 1;
    }
  
    // Returning the final answer
    return n - ans;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 2, 1 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findMin(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
      
    // Function to return the minimum number 
    // of elements to be removed from the ends 
    // of the array to make it sorted 
    static int findMin(int arr[], int n) 
    
      
        // To store the final answer 
        int ans = 1
      
        // Two pointer loop 
        for (int i = 0; i < n; i++) 
        
            int j = i + 1
      
            // While the array is increasing increment j 
            while (j < n && arr[j] >= arr[j - 1]) 
                j++; 
      
            // Updating the ans 
            ans = Math.max(ans, j - i); 
      
            // Updating the left pointer 
            i = j - 1
        
      
        // Returning the final answer 
        return n - ans; 
    
      
    // Driver code 
    public static void main (String[] args)
    
        int arr[] = { 3, 2, 1 }; 
        int n = arr.length; 
        System.out.println(findMin(arr, n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 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
def findMin(arr, n):
  
    # To store the final answer
    ans = 1
  
    # Two pointer loop
    for i in range(n):
        j = i + 1
  
        # While the array is increasing increment j
        while (j < n and arr[j] >= arr[j - 1]):
            j += 1
  
        # Updating the ans
        ans = max(ans, j - i)
  
        # Updating the left pointer
        i = j - 1
  
    # Returning the final answer
    return n - ans
  
# Driver code
arr = [3, 2, 1]
n = len(arr)
  
print(findMin(arr, n))
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.