> ## 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 Unsorted Continuous Subarray

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

LeetCode 581, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Stack](/catalog/topics/stack), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting), [Monotonic Stack](/catalog/topics/monotonic-stack). [View on LeetCode](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/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 581   # by problem number
lcpy gen -s shortest_unsorted_continuous_subarray   # by problem name
```

## Problem

Given an integer array `nums`, you need to find one **continuous subarray** such that if you only sort this subarray in non-decreasing order, then the whole array will be sorted in non-decreasing order.

Return *the shortest such subarray and output its length*.

### Examples

```
Input: nums = [2,6,4,8,10,9,15]
Output: 5
```

**Explanation:** You need to sort \[6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

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

```
Input: nums = [1]
Output: 0
```

### Constraints

* 1 \<= nums.length \<= 10^4
* -10^5 \<= nums\[i] \<= 10^5

**Follow up:** Can you solve it in `O(n)` time complexity?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def find_unsorted_subarray(self, nums: list[int]) -> int:
        n = len(nums)
        end = -2
        max_seen = nums[0]
        for i in range(1, n):
            if nums[i] < max_seen:
                end = i
            else:
                max_seen = nums[i]

        if end == -2:
            return 0

        start = 0
        min_seen = nums[n - 1]
        for i in range(n - 2, -1, -1):
            if nums[i] > min_seen:
                start = i
            else:
                min_seen = nums[i]

        return end - start + 1
```

## Complexity

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

## Tags


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