Source Code
#include <bits/stdc++.h>
using namespace std;
 
#define S second
#define F first
#define LL long long 
#define LINF 100000000000000000
#define INF 1000000000
 
const int N = 1e6+10;
const LL MOD = 1e9+7;

unordered_map<int,int>mp,mp1;
vector<int>vec;
int mx[N*2],mn[N*2];

int main()
{      
    //freopen("out.txt","w",stdout);

    int n;
    scanf("%d",&n);

    for(int i=1;i<=n;i++){
        int x;cin>>x;
        mx[x+N] = i;
        if(!mn[x+N])mn[x+N] = i;
    }

    int ans = 0;
    for(int i=1;i<=2e6;i++)
        if(mx[i]&&mn[i])ans = max(ans,mx[i]-mn[i]+1);

    printf("%d\n",ans);
}
Copy
Good Segment Hazem17
GNU G++17
380 ms
8.6 MB
Accepted