CSES - Shared codeLink to this code:
https://cses.fi/paste/408cea2327e7ddb2130854/
#include <bits/stdc++.h>
#define mod 1000000007;
#define ll long long
using namespace std;
int main()
{
ll n;
cin >> n;
ll arr[n];
for (int i = 0; i < n; ++i)
{
cin >> arr[i];
}
ll ans = 0;
ll start = 0, end = 0;
unordered_set<ll> set;
while (end < n )
{
if (set.find(arr[end]) == set.end())
{
set.insert(arr[end]);
ans = max(ans, end - start + 1);
end++;
}
else
{
set.erase(arr[start]);
start++;
}
}
cout << ans;
return 0;
}