forked from sourav-122/hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarray_sort_opt.cpp
More file actions
43 lines (37 loc) · 1.04 KB
/
subarray_sort_opt.cpp
File metadata and controls
43 lines (37 loc) · 1.04 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
#include <bits/stdc++.h>
using namespace std;
bool outOfOrder(vector<int> arr, int i){
int x = arr[i];
if(i==0) return x > arr[1];
if(i==arr.size()-1) return x < arr[i-1];
return x < arr[i-1] or x > arr[i+1];
}
pair<int, int> subarraySort(vector<int> arr){
int smallest = INT_MAX;
int largest = INT_MIN;
for(int i=0; i<arr.size(); i++){
int x = arr[i];
if(outOfOrder(arr, i)){
smallest = min(smallest, x);
largest = max(largest, x);
}
}
if(smallest==INT_MAX){
return {-1, -1};
}
int left = 0;
while(arr[left] <= smallest) left++;
int right = arr.size() - 1;
while(arr[right] >= largest) right--;
return {left, right};
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
vector<int> arr = {1, 2, 3, 4, 5, 8, 6, 7, 9, 10, 11};
auto p = subarraySort(arr);
cout << p.first << ", " << p.second << endl;
return 0;
}