Codeforces Round #835 (Div. 4) D. Challenging Valleys
One possible solution is to represent a range of equal element as a single element with that value. Construct this array b b b and loop through it and check how many element bi satisfy the conditions i = 0 i=0 i=0 or b i − 1 b_{i−1} bi−1< b i b_i bi and i = n − 1 i=n−1 i=n−1 or b i bi bi > b i + 1 b{i+1} bi+1. If exactly one index satisfies these conditions, print “YES” and othewise “NO”.
Complexity: O ( n ) O(n) O(n)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T=1;cin>>T;
while(T--)
{
int n;cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
n=unique(a.begin(),a.end())-a.begin();
if(n==1)
{
cout<<"YES"<<endl;
continue;
}
int res=0;
if(a[0]<a[1]) res++;
if(a[n-2]>a[n-1]) res++;
for(int i=1;i<n-1;i++)
if(a[i-1]>a[i] && a[i]<a[i+1])
res++;
if(res==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}