Source Code
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n, ans, a;
	cin >> n;
	ans = 1;
	for (int i = 0; i < 500001; i++) {
		pos[i][0] = pos[i][1] = -1;
		neg[i][0] = neg[i][1] = -1;
	}
	for (int i = 0; i < n; i++) {
		cin >> a;
		if (a >= 0) {
			if (pos[a][0] == -1)pos[a][0] = pos[a][1] = i;
			else pos[a][1] = i;
		}
		else if (a < 0) {
			if (neg[abs(a)][0] == -1)neg[abs(a)][0] = neg[abs(a)][1] = i;
			else neg[abs(a)][1] = i;
		}
	}
	for (int i = 0; i < 500001; i++) {
		ans = max(ans, pos[i][1] - pos[i][0] + 1);
		ans = max(ans, neg[i][1] - neg[i][0] + 1);
	}

	cout << ans;
	return 0;
}
Copy
Good Segment Mohamedmaher
GNU G++17
0 ms
0 KB
Compilation Error