Skip to contentKEEP THE CRAFTTHE SANCTUARY

Shortest Subarray to Remove for Sorted Order

Remove the shortest contiguous piece so that what is left is sorted.

The problem

Implement Solution.findLengthOfShortestSubarray(arr). You may remove one contiguous subarray from arr (it may be empty). The values that remain, in their original order, must be non-decreasing. Return the length of the shortest subarray you can remove. For example, [1, 2, 3, 10, 4, 2, 3, 5] gives 3: remove [10, 4, 2] to leave [1, 2, 3, 3, 5]. arr has between 1 and 100,000 values, each from 0 to 1,000,000,000.

Starter code

class Solution:
    def findLengthOfShortestSubarray(self, arr):
        pass

Public examples

  1. arr = [1, 2, 3, 10, 4, 2, 3, 5]gives3
  2. arr = [1, 2, 0, 3]gives1
  3. arr = [9]gives0

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.