Skip to content

Commit ceb871f

Browse files
authored
Create Shortest Subarray to be Removed to Make Array Sorted.java
1 parent f1e4d18 commit ceb871f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int findLengthOfShortestSubarray(int[] arr) {
3+
int right = arr.length - 1;
4+
while (right > 0 && arr[right] >= arr[right - 1]) {
5+
right--;
6+
}
7+
int result = right;
8+
int left = 0;
9+
while (left < right && (left == 0 || arr[left - 1] <= arr[left])) {
10+
while (right < arr.length && arr[left] > arr[right]) {
11+
right++;
12+
}
13+
result = Math.min(result, right - left - 1);
14+
left++;
15+
}
16+
return result;
17+
}
18+
}

0 commit comments

Comments
 (0)