Loading Similar Posts
The problem asks us to determine if a sequence is "special" - meaning no two adjacent numbers have the same parity (both odd or both even).
Your approach is correct and efficient: Check adjacent pairs for parity conflicts.
n
n
integersarr[i]
and arr[i+1]
:a = arr[i] % 2
(0 if even, 1 if odd)b = arr[i+1] % 2
(0 if even, 1 if odd)a == b
, the elements have the same parity → sequence is NOT special#include<bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
vector<int> arr(n);
for(int i = 0 ; i < n;i++) cin >> arr[i];
for(int i = 0 ; i < n-1;i++){
int a = arr[i]%2;
int b = arr[i+1]%2;
if (a == b) {
cout << "NO\n";
return;
}
}
cout << "YES\n";
}
int main(){
int t;
cin >>t;
while(t--){
solve();
}
}