-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonotonicArray.cpp
More file actions
45 lines (38 loc) · 953 Bytes
/
monotonicArray.cpp
File metadata and controls
45 lines (38 loc) · 953 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
34
35
36
37
38
39
40
41
42
43
44
45
/*An array is monotonic if it is either monotone increasing or monotone decreasing.
An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
Return true if and only if the given array A is monotonic.*/
#include<bits/stdc++.h>
using namespace std;
bool isMonotonic(vector<int>& A) {
bool isMonoInc=true;
bool isMonoDec=true;
for(int i=0; i<A.size()-1; i++)
{
if(A[i]>A[i+1]) //A[i] <= A[j] for inc, therefore false if A[i]>A[i+1]
{
isMonoInc= false;
break;
}
}
for(int i=0; i<A.size()-1; i++)
{
if(A[i]<A[i=1]) //A[i] >= A[j] for dec, therefore false if A[i]<A[i+1]
{
isMonoDec=false;
break;
}
}
return isMonoInc||isMonoDec;
}
int main(){
int n,x;
cin>>n;
vector<int> v;
for(int i=0; i<n; i++)
{
cin>>x;
v.push_back(x);
}
cout<<isMonotonic(v);
return 0;
}