Shortest Unsorted Continuous Subarray LeetCode Solution

Here, We see Shortest Unsorted Continuous Subarray LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Shortest Unsorted Continuous Subarray LeetCode Solution

Shortest Unsorted Continuous Subarray LeetCode Solution

Problem Statement

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.

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

Shortest Unsorted Continuous Subarray Leetcode Solution C++

class Solution {
public:
    int findUnsortedSubarray(vector<int>& nums) {
        int n = nums.size();
        int l = -1, r = -1;
        for(int i = 0; i < n - 1; i++) {
            if(nums[i] > nums[i + 1]) {
                l = i;
                break;
            }    
        }
        for(int i = n - 1; i >= 1; i--) {
            if(nums[i] < nums[i - 1]) {
                r = i;
                break;
            }
        }
        if(l == -1) return 0;
        int mini = nums[l], maxi = nums[l];
        for(int i = l; i <=r; i++) {
            mini = min(mini, nums[i]);
            maxi = max(maxi, nums[i]);
        }
        l = upper_bound(nums.begin(), nums.begin() + l, mini) - nums.begin();
        r = lower_bound(nums.begin() + r + 1, nums.end(), maxi) - nums.begin();
        return r - l;
    }
};Code language: PHP (php)

Shortest Unsorted Continuous Subarray Leetcode Solution Java

class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int len = nums.length - 1, left = -1, right = -1,
            max = nums[0], min = nums[len];
        for (int i = 1; i <= len; i++) {
            int a = nums[i], b = nums[len-i];
            if (a < max) right = i;
            else max = a;
            if (b > min) left = i;
            else min = b;
        }
        return Math.max(0, left + right - len + 1);
    }
}Code language: JavaScript (javascript)

Shortest Unsorted Continuous Subarray Solution JavaScript

var findUnsortedSubarray = function(nums) {
    let len = nums.length - 1, left = -1, right = -1,
        max = nums[0], min = nums[len]
    for (let i = 1; i <= len; i++) {
        let a = nums[i], b = nums[len-i]
        a < max ? right = i : max = a
        b > min ? left = i : min = b
    }
    return Math.max(0, left + right - len + 1)
};Code language: JavaScript (javascript)

Shortest Unsorted Continuous Subarray Solution Python

class Solution(object):
    def findUnsortedSubarray(self, nums):
        lenN, left, right = len(nums) - 1, -1, -1
        maxN, minN = nums[0], nums[lenN]
        for i in range(1, len(nums)):
            a, b = nums[i], nums[lenN-i]
            if a < maxN: right = i
            else: maxN = a
            if b > minN: left = i
            else: minN = b
        return max(0, left + right - lenN + 1)Code language: HTML, XML (xml)
Scroll to Top