Premium Only Content

1574. Shortest Subarray to be Removed to Make Array Sorted
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 <= 105
0 <= arr[i] <= 109
class Solution {
public:
int findLengthOfShortestSubarray(vector<int>& arr) {
int right = arr.size() - 1;
while (right > 0 && arr[right] >= arr[right - 1]) {
right--;
}
int ans = right;
int left = 0;
while (left < right && (left == 0 || arr[left - 1] <= arr[left])) {
while (right < arr.size() && arr[left] > arr[right]) {
right++;
}
ans = min(ans, right - left - 1);
left++;
}
return ans;
}
};
-
LIVE
Benny Johnson
1 hour agoHow We Got Jimmy Kimmel Ripped Off-Air, Why The Right Must Fight | Trump Press Conference LIVE Now
5,989 watching -
LIVE
The Big Migâ„¢
1 hour agoThe Lefts Nuclear Meltdown, Cancel Culture Cancels Jimmy Kimmel
6,022 watching -
LIVE
Chad Prather
50 minutes agoJimmy Kimmel Is FIRED After Horrific Charlie Kirk Comments + Trump Designates ANTIFA As Terrorists!
435 watching -
55:01
The White House
2 hours agoPress Conference with the Prime Minister of the United Kingdom of Great Britain and Northern Ireland
1.25K4 -
Barry Cunningham
59 minutes agoBREAKING NEWS: PRESIDENT TRUMP PARTICIPATES IN PRESS CONFERENCE IN ENGLAND
2 -
LIVE
Badlands Media
8 hours agoBadlands Daily: September 18, 2025
3,790 watching -
LIVE
JuicyJohns
2 hours ago $0.61 earned🟢#1 REBIRTH PLAYER 10.2+ KD🟢
52 watching -
DVR
Matt Kohrs
11 hours agoStocks Squeeze To New Highs 🚀🚀🚀 || Live Trading (OPEN, NVDA & TSLA)
11.8K2 -
45:23
Randi Hipper
1 hour agoBITCOIN PRICE MOMENTUM PICKS UP! CRITICAL LEVELS TO WATCH
4.72K1 -
11:33
Mrgunsngear
13 hours ago $4.03 earnedBreaking: This NFA Exempt Firearm Is Now Available With No Stamp - This Is HUGE 🇺🇸
8.3K21