-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumUnsortedSubarray.cpp
More file actions
57 lines (40 loc) · 1.16 KB
/
MaximumUnsortedSubarray.cpp
File metadata and controls
57 lines (40 loc) · 1.16 KB
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
53
54
55
56
/*
You are given an array (zero indexed) of N non-negative integers, A0, A1 ,…, AN-1.
Find the minimum sub array Al, Al+1 ,…, Ar so if we sort(in ascending order) that sub array, then the whole array should get sorted.
If A is already sorted, output -1.
Example :
Input 1:
A = [1, 3, 2, 4, 5]
Return: [1, 2]
Input 2:
A = [1, 2, 3, 4, 5]
Return: [-1]
In the above example(Input 1), if we sort the subarray A1, A2, then whole array A should get sorted.
LINK: https://www.interviewbit.com/problems/maximum-unsorted-subarray/
*/
vector<int> Solution::subUnsort(vector<int> &A) {
int n = A.size();
int f1 =0, f2 = 0, s = 0, e= n-1;
for(int i = 1; i<n; i++){
if(A[i]<A[i-1]){
int j = s;
while(j>=0 && A[j]>A[i]) j--;
if(j<s) s= j+1;
f1 = 1;
}
if(f1==0) s = i;
}
for(int i = n-2; i>=0; i--){
if(A[i]>A[i+1]){
int j = e;
while(j<n && A[j]<A[i]) j++;
if(j>e) e = j-1;
f2=1;
}
if(f2==0) e = i;
}
if(f1==0 || f2==0){
return {-1};
}
return {s,e};
}