Problem Info
Problem link here:
What I’ve Tried
I tried checking for the pattern of X_X and XX after 0 basing it:
My Work
#include<bits/stdc++.h>
using namespace std;
// note: lst1 refrs to the ones that have at least one
int main(){
int T, N;
cin >> T;
while(T--){
cin >> N;
int a[N];
bool good[N];
memset(good, 0, sizeof(good));
for(int i = 0; i < N; i++){
cin >> a[i];
a[i]--;
if (i > 0 && a[i] == a[i - 1] || i > 1 && a[i] == a[i - 2]) good[a[i]] = 1;
}
bool lst1 = 0;
for (int i = 0; i < N; i++){
if(good[i]){
lst1 = 1;
cout << i + 1 << " ";
}
}
if (!lst1) cout << -1;
cout << endl;
}
}
Explanationn:
- Read the array
aand convert each element to 0-based. - While reading, check each element
a[i]:
- If it equals the previous element (
a[i] == a[i-1]), or - If it equals the element two spots earlier (
a[i] == a[i-2]) then it marks the value as good
- After processing the whole array, print all values that were marked good then convert back to 1-base index
- If none are good, print
-1.

