-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_Kalindrome_Array.cpp
More file actions
51 lines (44 loc) · 1.05 KB
/
Copy pathB_Kalindrome_Array.cpp
File metadata and controls
51 lines (44 loc) · 1.05 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
44
45
46
47
48
49
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = a; i < b; i++)
bool solve(vector<int>& arr , int l , int r , int n) {
while(l<=r){
while(l<n && arr[l]==n) l++;
while(r>=0 && arr[r]==n) r--;
if(l<=r && arr[l]!=arr[r]) return false;
else{
l++;
r--;
}
}
return true;
}
int main() {
fastio;
int t;
cin >> t;
while (t--) {
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
int l = 0 , r = n-1;
while(l<=n && arr[l]==arr[r]){
l++;
r--;
}
//cout<<arr[l]<<" "<<arr[r]<<endl;
bool check = solve(arr , l , r , arr[l]);
if(!check) check = solve(arr,l , r , arr[r]);
if(check) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}