Skip to content
Related Articles

Related Articles

Minimum elements to be removed from the ends to make the array sorted
  • Last Updated : 06 Dec, 2020

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:
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:

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++




// 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;
}


Java




// 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


Python3




# 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


C#




// 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


Output: 

2

 

Time Complexity: O(N )

Auxiliary Space: O(1)

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
Recommended Articles
Page :