-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
33 lines (29 loc) · 685 Bytes
/
main.cpp
File metadata and controls
33 lines (29 loc) · 685 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
#include <iostream>
#include <vector>
#include <initializer_list>
int findUnsortedSubarray(const std::vector<int>& nums) {
int i = 0;
int j = -1;
int left = 0;
int right = nums.size() - 1;
int min = INT_MAX;
int max = INT_MIN;
while (right >= 0) {
max = std::max(nums[left], max);
if (nums[left] != max) {
j = left;
}
min = std::min(nums[right], min);
if (nums[right] != min) {
i = right;
}
left++;
right--;
}
return j - i + 1;
}
int main() {
int i = findUnsortedSubarray({2,6,4,8,10,9,15});
std::cout << i << std::endl;
return 0;
}