Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Shortest Subarray to be Removed to Make Array Sorted

XPChallenge Points: 20
levelLevel: Medium

You are given an array of integers.
You are allowed to remove one continuous part (subarray) from it.
Your goal is that the remaining array should be in non-decreasing (sorted) order.

Return the minimum length of the subarray you need to remove.
Removing an empty subarray is allowed — which means if the array is already sorted, answer is 0.

Example 1:

Input: arr = [2, 3, 9, 8, 7, 10]

Output: 3

Explanation:

Remove [9, 8, 7], remaining → [2, 3, 10] which is sorted.

Example 2:

Input: arr = [4, 5, 6, 7]

Output: 0

Explanation:

Already sorted → nothing to remove.

Example 3:

Input: arr = [10, 8, 6, 4, 2]

Output: 4

Explanation:

Only one element can remain sorted, so remove 4 elements.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result