Source Code
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <math.h>

#define pairi pair <int,int>
#define pairll pair <long long,long long>
#define vi vector <int> 
#define vll vector <long long> 
#define mapi map <int,int> 
#define mapill map <int,long long> 
#define seti set <int> 
#define setll set <long long> 
#define pb push_back
#define fr(i,a,b) for(int i = a ; i < b ; i++)
#define fir(i,a,b) for(int i = b ; i >= a ; i--)
typedef long long ll ;


using namespace std;

int m[1000006] ;

int main()
{
    int t , n , i , k , x , *a ;
    t = 1 ;
    while(t--)
    {
        cin >> n ;
        a = new int[n] ;
        k = 1 ;
        fr(i,0,1000006)
            m[i] = 0 ;
        fr(i,0,n)
        {
            cin >> a[i] ;
            if(i == 0)
                m[a[i]+500000] = -1 ;
            if(m[a[i]+ 500000] == 0)
                m[a[i]+500000] = i ;
            else
                k = max(k,i-m[a[i]+500000]+1 - (m[a[i]+500000] == -1 )) ;
        }
        cout << k << endl ;
    }
}
Copy
Good Segment Mahmoud
GNU G++17
429 ms
8.5 MB
Accepted