-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMaximum Unsorted Subarray.cpp
More file actions
52 lines (38 loc) · 925 Bytes
/
Maximum Unsorted Subarray.cpp
File metadata and controls
52 lines (38 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
vector<int> Solution::subUnsort(vector<int> &A) {
int n = A.size();
int st = 0,en = n-1;
for(int i=1;i<n;i++){
if(A[i]<A[i-1]){
st=i-1;
break;
}
}
for(int i=n-2;i>=0;i--){
if(A[i]>A[i+1]){
en = i+1;
break;
}
}
if(st==0 && en == n-1){
return vector<int>(1,-1);
}
int min_el = *min_element(A.begin()+st,A.begin()+en+1);
int max_el = *max_element(A.begin()+st,A.begin()+en+1);
int bg =-1,sm=n;
for(int i = 0;i<st;i++){
if(A[i]>min_el){
bg =i;
break;
}
}
for(int i = n-1;i>en;i--){
if(A[i]<max_el){
sm =i;
break;
}
}
if(bg!=-1){st=bg;}
if(sm!=n){en = sm;}
vector<int> op;op.push_back(st);op.push_back(en);
return op;
}