Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.

Return the shortest such subarray and output its length.

&nbsp;
Example 1:

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.


Example 2:

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


Example 3:

Input: nums = [1]
Output: 0


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 104
	-105 &lt;= nums[i] &lt;= 105


&nbsp;
Follow up: Can you solve it in O(n) time complexity?
#lets start adding elements to stack. We have to fin the min length [a, b] interval (corresponding to the problem description).
# a has to be the first element's index we pop from the array. lets say y is the last element's index we pop.
# and max_pop is the maximum element(not index) we pop during stacking.After stacking process is done we are going to have
#last elements in the stack E(E is the stack after stacking is done).We have to find M = maximum_element(max_pop, all elements of E)
#Index of M is going to be right edge of the [a, b] interval

class Solution:
    def findUnsortedSubarray(self, nums) -> int:
        stack = []
        min_index = len(nums)
        max_index = 0
        max_pop = float('-inf')
        for i in range(len(nums)):
            while stack and nums[i] < stack[-1][0]:

                p = stack.pop()
                if p[0] > max_pop:
                    max_pop = p[0]
                if p[1] < min_index:
                    min_index = p[1]
                if p[1] > max_index:
                    max_index = p[1]
            stack.append([nums[i], i])
        max_r = max_index
        for st in stack:
            if st[0] < max_pop:
                max_r = st[1]
        if min_index == len(nums):
            return 0
        return max_r - min_index +1
class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int[] numsClone = nums.clone();
        Arrays.sort(nums);

        int s = Integer.MAX_VALUE;
        int e = Integer.MIN_VALUE;

        for(int i = 0; i<nums.length; i++) {
            if(numsClone[i] != nums[i]) {
                s = Math.min(s, i);
                e = Math.max(e, i);
            }
        }

        if(s == Integer.MAX_VALUE || e == Integer.MIN_VALUE) {
            return 0;
        }

        return e-s+1;
    }
}
class Solution {
public:
    int max_value = 1000000;
    int min_value = -1000000;
    
    int findUnsortedSubarray(vector<int>& nums) {
		// max_list refers to: the max value on the left side of the current element
		// min_list refers to: the min value on the right side of the current element
        vector<int> max_list(nums.size(), 0);
        vector<int> min_list(nums.size(), 0);
        min_list[nums.size()-1] = max_value;
        max_list[0] = min_value;
        
        // init two lists
        for(int i=nums.size()-2; i>=0; i--)
            min_list[i] = min(min_list[i+1], nums[i+1]);
        
        for(int i=1; i<nums.size(); i++) 
            max_list[i] = max(max_list[i-1], nums[i-1]);
        
        // get left bound
        int left = 0;
        while(left < nums.size() && min_list[left] >= nums[left])
            left++;
        
        // get right bound
        int right = nums.size() - 1;
        while(right >= 0 && max_list[right] <= nums[right])
            right--;
        
        if (left == nums.size()) // monotonic ascending array
            return 0;
        else
            return (right-left+1);
    }
};
var findUnsortedSubarray = function(nums) {
    let left = 0;
    let right = nums.length - 1;
    const target = [...nums].sort((a, b) => a - b);

    while (left < right && nums[left] === target[left]) left += 1;
    while (right > left && nums[right] === target[right]) right -= 1;
    const result = right - left;

    return result === 0 ? 0 : result + 1;
};
Shortest Unsorted Continuous Subarray