Skip to content

Commit 437577e

Browse files
authored
Create 1574. Shortest Subarray to be Removed to Make Array Sorted (#636)
2 parents 744470d + 17dba61 commit 437577e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
int findLengthOfShortestSubarray(vector<int>& arr) {
4+
//find first ans last decreasing pair
5+
int first=-1;
6+
int last=-1;
7+
int n=arr.size();
8+
for(int i=0;i<n-1;i++){
9+
if(arr[i]>arr[i+1]){
10+
if(first==-1){
11+
first=i;
12+
}
13+
last=i;
14+
}
15+
}
16+
if (first == -1) {
17+
return 0;
18+
}
19+
// now find the max len of non decreasing array that does not has these pair
20+
21+
int ans=min(last+1,n-first-1);
22+
for(int i=0;i<=first;i++){
23+
for(int j=last+1;j<n;j++){
24+
if(arr[i]<=arr[j]){
25+
ans=min(ans,j-i-1);
26+
break;
27+
}
28+
}
29+
}
30+
return ans;
31+
}
32+
};
33+
34+

0 commit comments

Comments
 (0)