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;
}
};
-
2:08:18
Redacted News
2 hours agoBOMBSHELL! TRUMP SHUTDOWN ISRAEL'S PLANS FOR WAR WITH IRAN, TRUMP SLAMS FED JEROME POWELL | REDACTED
79.7K53 -
1:13:52
Dr. Drew
4 hours agoThe RFK Files Coverup & Failed State Of California w/ Mark Groubert & Chris Moritz – Ask Dr. Drew
9.59K -
1:15:43
vivafrei
3 hours agoCanada Debate RECAP! Logan Act Violation? FSU Shooting! Real ID or Big Brother Surveillance? & MORE!
43.6K29 -
45:29
Candace Show Podcast
2 hours agoBlake Lively Is Time's Most Influential | Katy Perry Goes To Space | Candace Ep 178
32.2K74 -
2:11:01
The Quartering
5 hours agoActive Shooter At Florida University, Karmelo Anthony Parents Press Conference, Gaming Gets Worse
124K75 -
LIVE
LFA TV
20 hours agoALL DAY LIVE STREAM 4/17/25
665 watching -
42:24
Stephen Gardner
3 hours ago🔴Trump Urged to STOP Ukraine’s CASH GRAB NOW! - Col. Douglas Macgregor
39.2K31 -
1:07:07
Michael Malice
1 day ago"YOUR WELCOME" with Michael Malice #359: Douglas Murray
39.2K21 -
1:26:58
The HotSeat
3 hours agoPay Attention, This IS The ONLY Thing That Matters This Week!
21.5K3 -
1:06:09
Jeff Ahern
3 hours ago $0.61 earnedThursday Thrash with Jeff Ahern
17.1K