Help on Majority Opinion

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:

  1. Read the array a and convert each element to 0-based.
  2. 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
  1. After processing the whole array, print all values that were marked good then convert back to 1-base index
  2. If none are good, print -1.
1 Like

The USACO output: :arrow_right:


My sublime text build: :arrow_right:


I was just wondering why the output in the USACO judge was the same as the expected one, but it still shows that I got it wrong?

1 Like

it may have been a spacing error

1 Like

Yes, see Input & Output · USACO Guide for more info

2 Likes

ye check if the input has any unwanted spaces or lines

tysm @Benq
i will fix it