#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e6;
set<int>big;
int a[MAXN];
set<int>values;
int n;
map<int,vector<int> >where;
int main()
{
    cin>>n;
    int ans=-1;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        where[a[i]].push_back(i);
        values.insert(a[i]);
    }
    set<int>::iterator it=values.end();
    big.insert(0);
    big.insert(-1);
    big.insert(n+2);
    big.insert(n+1);
    while(it!=values.begin())
    {
        it--;
        for(int i=0;i<where[(*it)].size();i++)big.insert(where[(*it)][i]);
        for(int i=0;i<where[(*it)].size();i++)
        {
            set<int>::iterator it1=big.lower_bound(where[(*it)][i]);
            int l,r,len;
            r=(*it1);
            it1--;l=(*it1);it1++;
            len=r-l+1;
            //cout<<where[(*it)][i]<<" "<<l<<" "<<r<<endl;
            if(l>=1&&r<=n)ans=max(ans,(*it)-len);
            it1++;r=(*it1);it1--;
            l=(*it1);
            len=r-l+1;
            //cout<<where[(*it)][i]<<" "<<l<<" "<<r<<endl;
            if(l>=1&&r<=n)ans=max(ans,(*it)-len);
        }
    }
    cout<<ans<<endl;
return 0;
}
