# 581

#time: O(N log N)
# space: O(N)
class Solution:
    def shortest_subarray(self, nums):
        if not nums:
            return []
        
        s_nums = sorted(nums)
        left, right = len(nums) - 1, 0

        for i in range(len(nums)):
            if nums[i] != s_nums[i]:
                left = min(left, i)
                right = max(right, i)
        
        return right - left + 1 if right >= left else 0

nums = [2, 6, 4, 8, 10, 9, 15]
solution = Solution()
print(solution.shortest_subarray(nums))