Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Input: [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.

const findUnsortedSubarray = nums =>
nums
.slice()
.sort((a, b) => a - b)
.reduce((acc, curr, idx) => acc + (curr === nums[idx] ? ' ' : 'x'), '')
.trim().length;
def findUnsortedSubarray(self, nums: List[int]) -> int:
is_same = [a == b for a, b in zip(nums, sorted(nums))]
return 0 if all(is_same) else len(nums) - is_same.index(False) - is_same[::-1].index(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.