> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Shortest Subarray to be Removed to Make Array

> Tested Python solution for LeetCode 1574 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1574, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Binary Search](/catalog/topics/binary-search), [Stack](/catalog/topics/stack), [Monotonic Stack](/catalog/topics/monotonic-stack). [View on LeetCode](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1574   # by problem number
lcpy gen -s shortest_subarray_to_be_removed_to_make_array_sorted   # by problem name
```

## Problem

Given an integer array `arr`, remove a subarray (can be empty) from `arr` such that the remaining elements in `arr` are **non-decreasing**.

Return the length of the shortest subarray to remove.

A **subarray** is a contiguous subsequence of the array.

### Examples

```
Input: arr = [1,2,3,10,4,2,3,5]
Output: 3
```

**Explanation:** The shortest subarray we can remove is \[10,4,2] of length 3. The remaining elements after that will be \[1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray \[3,10,4].

```
Input: arr = [5,4,3,2,1]
Output: 4
```

**Explanation:** Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either \[5,4,3,2] or \[4,3,2,1].

```
Input: arr = [1,2,3]
Output: 0
```

**Explanation:** The array is already non-decreasing. We do not need to remove any elements.

### Constraints

* 1 \<= arr.length \<= 10^5
* 0 \<= arr\[i] \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shortest_subarray_to_be_removed_to_make_array_sorted/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shortest_subarray_to_be_removed_to_make_array_sorted/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def find_length_of_shortest_subarray(self, arr: list[int]) -> int:
        n = len(arr)
        end = n - 1
        while end > 0 and arr[end - 1] <= arr[end]:
            end -= 1
        result = end
        start = 0
        while start < end and (start == 0 or arr[start - 1] <= arr[start]):
            while end < n and arr[end] < arr[start]:
                end += 1
            result = min(result, end - start - 1)
            start += 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.