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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ implementation of the approach ` `#include ` `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 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 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# 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
```

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 :