-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathsubarray-sort.cpp
57 lines (53 loc) · 1.37 KB
/
subarray-sort.cpp
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
57
#include <vector>
#include <iostream>
using namespace std;
int findCorrectSpot(int elem, bool fromFront, vector<int>& array) {
int result;
if (fromFront) {
for (int i = 0; i < array.size(); i++) {
if (elem < array[i]) {
result = i;
break;
}
}
} else {
for (int k = array.size() - 1; k >= 0; k--) {
if (elem > array[k]) {
result = k;
break;
}
}
}
return result;
}
vector<int> subarraySort(vector<int> array) {
// Write your code here.
int smallU = INT_MAX;
int largeU = INT_MIN;
vector<int> result = {-1, -1};
for (int i = 0; i < array.size(); i++) {
if (i == 0) {
if (array[i] > array[i+1]) {
if (smallU > array[i]) smallU = array[i];
if (largeU < array[i]) largeU = array[i];
}
}
if (i > 0 && i < array.size() - 1) {
if (array[i] > array[i+1] || array[i] < array[i-1]) {
if (smallU > array[i]) smallU = array[i];
if (largeU < array[i]) largeU = array[i];
}
}
if (i == array.size() - 1) {
if (array[i] < array[i-1]) {
if (smallU > array[i]) smallU = array[i];
if (largeU < array[i]) largeU = array[i];
}
}
}
if (smallU == INT_MAX && largeU == INT_MIN) return {-1, -1};
int left = findCorrectSpot(smallU, true, array);
int right = findCorrectSpot(largeU, false, array);
cout << left << ", " << right << endl;
return {left, right};
}