
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find shortest subarray to be removed to make array sorted in Python
Suppose we have an array called arr, we have to remove a subarray of arr such that the remaining elements in arr are in non-decreasing order. We have to find the length of the shortest subarray to remove.
So, if the input is like arr = [10,20,30,100,40,20,30,50], then the output will be 3 because we can remove [100, 40, 20] which is smallest subarray of length 3, and by removing these all are in non-decreasing order [10,20,30,30,50].
To solve this, we will follow these steps:
- n := size of arr
- arr := insert 0 at the left of arr and infinity at the right of arr
- A,B := two new empty lists
- p := 1, q:= size of arr -2
- M := 0
- while p <= q, do
- if arr[p-1] <= arr[p], then
- insert arr[p] at the end of A
- p := p + 1
- otherwise when arr[q] <= arr[q+1], then
- insert arr[q] at the end of B
- while A is not empty and last element of A > last element of B, do
- delete last element from A
- q := q - 1
- otherwise,
- come out from loop
- M := maximum of M and size of A + size of B
- if arr[p-1] <= arr[p], then
- return n - M
Let us see the following implementation to get better understanding:
Example
def solve(arr): n = len(arr) arr = [0] + arr + [float("inf")] A,B=[],[] p,q=1,len(arr)-2 M = 0 while p <= q: if arr[p-1] <= arr[p]: A.append(arr[p]) p += 1 elif arr[q] <= arr[q+1]: B.append(arr[q]) while A and A[-1] > B[-1]: A.pop() q -= 1 else: break M = max(M, len(A)+len(B)) return n - M arr = [10,20,30,100,40,20,30,50] print(solve(arr))
Input
[10,20,30,100,40,20,30,50]
Output
3
- Related Articles
- Program to find max chunks to make array sorted in Python
- Program to find shortest sublist so after sorting that entire list will be sorted in Python
- Program to find range sum of sorted subarray sums using Python
- Program to count minimum invalid parenthesis to be removed to make string correct in Python
- Program to find minimum number of operations to make string sorted in Python
- Program to find number of elements can be removed to make odd and even indexed elements sum equal in Python
- Program to find ways to make a fair array in Python
- Program to find minimum moves to make array complementary in Python
- Shortest Unsorted Continuous Subarray in Python
- Program to find length of shortest supersequence in Python
- Program to find sum of absolute differences in a sorted array in Python
- Program to find minimum operations to make array equal using Python
- Delete Columns to Make Sorted in Python
- Program to find maximum subarray min-product in Python
- Program to find shortest cycle length holding target in python

Advertisements