Shortest Unsorted Continuous Subarray

Medium

Description

Given an integer array nums, find the shortest continuous subarray that, when sorted, results in the whole array being sorted.

Examples

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

Sort [6,4,8,10,9].

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

Edge case returning zero.

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

Edge case returning zero.

Constraints

  • 1 ≤ nums.length ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!