Source Code
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int M = 2e6 + 2, shift = 5e5;
    int n, last[M] = {}, first[M], ans = 1;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        x += shift;
        if (!last[x])
            first[x] = i;
        last[x] = i;
    }

    for (int i = 0; i < M; i++)
        if (last[i])
            ans = max(ans, last[i] - first[i] + 1);
    cout << ans << '\n';
}
Copy
Good Segment ahmad_salah
GNU G++17
239 ms
16.5 MB
Accepted