#include <bits/stdc++.h>

int limit = 0;
using namespace std;
int n,m,gtln,d,x;
int dd[1000001];
void Read_Int(int &x)
{
    char c;
    bool neg=false;
    for (c=getchar();c!='-'&&(c<'0'||c>'9');c=getchar());
    if (c=='-') {neg=true;c=getchar();}
    x= c-'0';
    for (c=getchar();c>='0'&&c<='9';c=getchar())
    {
        x=x*10+c-'0';
    }
    if (neg==true) x=(-1)*x;
}
void Write_Int(int x)
{
    if (x<0) {putchar('-');x=(-1)*x;}
    if (x>9) Write_Int(x/10);
    putchar(x%10+'0');
}

int main()
{
	freopen("LSEQ.inp","r",stdin);
	freopen("LSEQ.out","w",stdout);
	ios_base::sync_with_stdio(false);
    Read_Int(m);
    for (int i=0;i<m;i++)
    {
        Read_Int(x);
        limit=max(limit,x);
        dd[x]=1;
        if (x==0) d++;
    }
    for (int i=0;i<limit;i++)
    {
        dd[i]=1-dd[i];
    }
    for (int i=1;i<limit;i++)
    {
        dd[i]+=dd[i-1];
    }
    int i=0,j=1;
    while (i<=limit&&j<=limit+1)
    {
        if (dd[j]-dd[i]<=d)
        {
            gtln=max(gtln,j-i);
            j++;
        }
        else i++;
    }
    Write_Int(gtln);
}

