Link to this code:
https://cses.fi/paste/d65fa65e76c228ed26a32d//*
author: @ankingcodes
created: 2021-07-18 09:12:58.312279
*/
#include<bits/stdc++.h>
#include<algorithm>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define MOD 1000000007
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> PBDS;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
tree_order_statistics_node_update> pairPBDS;
void print(vector<ll> a) {
cout << "VECTOR: ";for (auto x: a) cout << x << " "; cout << endl;
}
void print(set<ll> a) {
cout << "SET: ";for (auto x: a) cout << x << " "; cout << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n; cin >> n;
vector<ll> a(n);
for (auto &i: a) cin >> i;
if (a.size() == 1) {
cout << 1 << endl;
return 0;
}
ll i=0, j=-1;
ll mx = INT_MIN;
set<ll> s;
for (int i=0;i<n;i++) {
while (j < n-1 && !s.count(a[j+1])) s.insert(a[++j]);
mx = max(mx, j - i + 1);
s.erase(a[i]);
}
cout << mx << endl;
return 0;
}