Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions 1574. Shortest Subarray to be Removed to Make Array Sorted
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
int findLengthOfShortestSubarray(vector<int>& arr) {
//find first ans last decreasing pair
int first=-1;
int last=-1;
int n=arr.size();
for(int i=0;i<n-1;i++){
if(arr[i]>arr[i+1]){
if(first==-1){
first=i;
}
last=i;
}
}
if (first == -1) {
return 0;
}
// now find the max len of non decreasing array that does not has these pair

int ans=min(last+1,n-first-1);
for(int i=0;i<=first;i++){
for(int j=last+1;j<n;j++){
if(arr[i]<=arr[j]){
ans=min(ans,j-i-1);
break;
}
}
}
return ans;
}
};


Loading