-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIncreasing_Triplet_Subsequence.cpp
66 lines (64 loc) · 1.74 KB
/
Increasing_Triplet_Subsequence.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
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int x;
vector<int> nums;
for(int i = 0; i < n; i++)
{
cin>>x;
nums.push_back(x);
}
if(n == 3)
{
if(nums[0] < nums[1] && nums[1] < nums[2])
return true;
else
return false;
}
int mnindex = min_element(nums.begin(), nums.end()) - nums.begin();
int mxindex = max_element(nums.begin(), nums.end()) - nums.begin();
int mn = *min_element(nums.begin(), nums.end());
int mx = *max_element(nums.begin(), nums.end());
if(mnindex < mxindex && (mxindex - mnindex) > 0 && (mx - mn) > 1)
{
cout<<"True"<<endl;
return 0;
}
int prefix[n], suffix[n];
memset(prefix, INT_MAX, sizeof(prefix));
memset(suffix, INT_MIN, sizeof(suffix));
prefix[0] = nums[0];
suffix[n-1] = nums[n-1];
for(int i = 1; i < n; i++)
{
prefix[i] = min(nums[i], prefix[i-1]);
}
for(int i = n-2; i >= 0; i--)
{
suffix[i] = max(nums[i], suffix[i+1]);
}
bool f = false;
/*
for(int i = 0; i < n; i++)
{
cout<<prefix[i]<<" "<<suffix[i]<<endl;
}
*/
for(int i = 0; i < n; i++)
{
if(nums[i] != prefix[i] && nums[i] != suffix[i] &&
nums[i]>prefix[i] && nums[i]<suffix[i]
&& prefix[i] != suffix[i])
{
f = true;
break;
}
}
if(f)
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}