Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array arr, remove a subarray (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:
Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4].
Example 2:
Input: arr = [5,4,3,2,1] Output: 4 Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1].
Example 3:
Input: arr = [1,2,3] Output: 0 Explanation: The array is already non-decreasing. We do not need to remove any elements.
Constraints:
1 <= arr.length <= 1050 <= arr[i] <= 109Problem summary: Given an integer array arr, remove a subarray (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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search · Stack
[1,2,3,10,4,2,3,5]
[5,4,3,2,1]
[1,2,3]
count-the-number-of-incremovable-subarrays-ii)count-the-number-of-incremovable-subarrays-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1574: Shortest Subarray to be Removed to Make Array Sorted
class Solution {
public int findLengthOfShortestSubarray(int[] arr) {
int n = arr.length;
int i = 0, j = n - 1;
while (i + 1 < n && arr[i] <= arr[i + 1]) {
++i;
}
while (j - 1 >= 0 && arr[j - 1] <= arr[j]) {
--j;
}
if (i >= j) {
return 0;
}
int ans = Math.min(n - i - 1, j);
for (int l = 0; l <= i; ++l) {
int r = search(arr, arr[l], j);
ans = Math.min(ans, r - l - 1);
}
return ans;
}
private int search(int[] arr, int x, int left) {
int right = arr.length;
while (left < right) {
int mid = (left + right) >> 1;
if (arr[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
// Accepted solution for LeetCode #1574: Shortest Subarray to be Removed to Make Array Sorted
func findLengthOfShortestSubarray(arr []int) int {
n := len(arr)
i, j := 0, n-1
for i+1 < n && arr[i] <= arr[i+1] {
i++
}
for j-1 >= 0 && arr[j-1] <= arr[j] {
j--
}
if i >= j {
return 0
}
ans := min(n-i-1, j)
for l := 0; l <= i; l++ {
r := j + sort.SearchInts(arr[j:], arr[l])
ans = min(ans, r-l-1)
}
return ans
}
# Accepted solution for LeetCode #1574: Shortest Subarray to be Removed to Make Array Sorted
class Solution:
def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
n = len(arr)
i, j = 0, n - 1
while i + 1 < n and arr[i] <= arr[i + 1]:
i += 1
while j - 1 >= 0 and arr[j - 1] <= arr[j]:
j -= 1
if i >= j:
return 0
ans = min(n - i - 1, j)
for l in range(i + 1):
r = bisect_left(arr, arr[l], lo=j)
ans = min(ans, r - l - 1)
return ans
// Accepted solution for LeetCode #1574: Shortest Subarray to be Removed to Make Array Sorted
struct Solution;
impl Solution {
fn find_length_of_shortest_subarray(arr: Vec<i32>) -> i32 {
let n = arr.len();
let mut l = 0;
while l + 1 < n && arr[l] <= arr[l + 1] {
l += 1;
}
if l == n - 1 {
return 0;
}
let mut r = n - 1;
while r > 0 && arr[r - 1] <= arr[r] {
r -= 1;
}
let mut res = (n - 1 - l).min(r);
let mut i = 0;
let mut j = r;
while i <= l && j < n {
if arr[i] <= arr[j] {
res = res.min(j - i - 1);
i += 1;
} else {
j += 1;
}
}
res as i32
}
}
#[test]
fn test() {
let arr = vec![1, 2, 3, 10, 4, 2, 3, 5];
let res = 3;
assert_eq!(Solution::find_length_of_shortest_subarray(arr), res);
let arr = vec![5, 4, 3, 2, 1];
let res = 4;
assert_eq!(Solution::find_length_of_shortest_subarray(arr), res);
let arr = vec![1, 2, 3];
let res = 0;
assert_eq!(Solution::find_length_of_shortest_subarray(arr), res);
let arr = vec![1];
let res = 0;
assert_eq!(Solution::find_length_of_shortest_subarray(arr), res);
}
// Accepted solution for LeetCode #1574: Shortest Subarray to be Removed to Make Array Sorted
function findLengthOfShortestSubarray(arr: number[]): number {
let [l, r, n] = [0, arr.length - 1, arr.length];
while (r && arr[r - 1] <= arr[r]) r--;
if (r === 0) return 0;
let ans = r;
while (l < r && (!l || arr[l - 1] <= arr[l])) {
while (r < n && arr[l] > arr[r]) r++;
ans = Math.min(ans, r - l - 1);
l++;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.