Shortest Subarray to be Removed to Make Array Sorted

Given an integer array arr, remove a subarray (which can be empty) from arr such that the remaining elements in arr are non-decreasing.

Return the length of the shortest subarray to remove.

A subarray is a contiguous subsequence of the array.

Example 1
Inputarr = [1,2,3,10,4,2,3,5]
Output3
The shortest subarray we can remove is [10,4,2] of length 3, leaving [1,2,3,3,5] sorted; another correct solution is to remove [3,10,4].
Example 2
Inputarr = [5,4,3,2,1]
Output4
Since the array is strictly decreasing, we can only keep a single element, so we need to remove a subarray of length 4.

Constraints

  • 1 <= arr.length <= 10^5
  • 0 <= arr[i] <= 10^9

Asked at 9 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate