↓Skip to main content
  1. LeetCode/

LeetCode 1574: Shortest Subarray to be Removed to Make Array Sorted

·2 mins· ·
LeetCode Medium Two-Pointers Binary-Search Array Monotonic-Stack
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-25

  • Submissions: Accepted (first try)

Approach
#

After removing one contiguous subarray, what remains is a prefix plus a suffix; both must be non-decreasing, and so must the join.

  1. Find the longest non-decreasing prefix arr[0..left] from the left. If it is the whole array, the answer is 0.
  2. Find the longest non-decreasing suffix arr[right..n-1] from the right.
  3. Three ways to keep elements:
    • Keep only the prefix: remove n - left - 1.
    • Keep only the suffix: remove right.
    • Keep the prefix up to i and the suffix from j, which needs arr[i] <= arr[j]: remove j - i - 1.

The third case uses same-direction pointers: i goes from 0 to left, j starts at right. If arr[i] <= arr[j], update the answer and advance i (try a longer prefix); otherwise advance j (larger suffix values). Since arr[i] only grows, the matching j only moves right, and neither pointer moves back.

My Solution
#

class Solution:
    def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
        n = len(arr)
        left = 0
        while left + 1 < n and arr[left] <= arr[left + 1]:
            left += 1
        if left == n - 1:
            return 0                               # already sorted

        right = n - 1
        while right > 0 and arr[right - 1] <= arr[right]:
            right -= 1

        ans = min(n - left - 1, right)             # keep only the prefix / only the suffix
        i, j = 0, right
        while i <= left and j < n:
            if arr[i] <= arr[j]:
                ans = min(ans, j - i - 1)          # prefix up to i, suffix from j
                i += 1
            else:
                j += 1
        return ans

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Forgetting the “only prefix” and “only suffix” cases.
  • The join condition is arr[i] <= arr[j] (non-decreasing), not <.

Interview Explanation
#

After removing one block, what remains is a prefix plus a suffix, so I find the longest non-decreasing prefix and suffix. I start with the better of keeping only the prefix or only the suffix, then use two same-direction pointers to join prefix index i with suffix index j: if arr[i] <= arr[j], update the answer and advance i, otherwise advance j. Neither pointer moves back, so time is O(n).

Related

LeetCode 2563: Count the Number of Fair Pairs
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 2563 note: split “sum in [lower, upper]” into “sum <= upper” minus “sum <= lower - 1”, and count each with converging pointers over the sorted array in O(n)
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 1498 note: only the minimum and maximum matter, so order does not and we can sort; fixing nums[left] as the minimum, the elements between are free, giving 2^(right - left) choices
LeetCode 825: Friends Of Appropriate Ages
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 825 note: after sorting, the people someone can invite have ages in (0.5 × age + 7, age], a contiguous range kept with two same-direction pointers, minus the person themself
LeetCode 611: Valid Triangle Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Greedy
LeetCode 611 note: sort, fix the longest side k, and use two pointers for the shorter sides; when the sum is large enough every i in between works, adding j - i at once
LeetCode 287: Find the Duplicate Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Bit-Manipulation
LeetCode 287 note: treat i -> nums[i] as a linked list; the duplicate is the cycle entry, found with Floyd’s algorithm in O(1) space without modifying the array
LeetCode 167: Two Sum II - Input Array Is Sorted
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search
LeetCode 167 note: sorted, so move two pointers inward, shifting right when the sum is too large and left when too small; indices are 1-based