#include <iostream>
#include <climits>
#define MAX_N 1000006
using namespace std;
pair <int, int> table[2][MAX_N];
int nums[MAX_N];

int main()
{
    int n;
    cin >> n;

    for(int i=0; i<n; i++)
    {
        cin >> nums[i];
    }
    int index=0;
    int maxi=INT_MIN;
    int curr;
    for(int i=0; i<n-1; i++)
    {
        table[index][i].first=max(nums[i], nums[i+1]);
        table[index][i].second=min(nums[i], nums[i+1]);
        curr=table[index][i].second-2;
        if(curr>maxi)
        {
            maxi=curr;
        }
    }

    int pindex=1;
    for(int i=3; i<n; i++)
    {
        index++;
        index%=2;
        pindex++;
        pindex%=2;
        for(int j=0; j<=n-i; j++)
        {
            table[index][j].first=max(table[pindex][j].first, table[pindex][j+1].first);
            table[index][j].second=max(table[pindex][j].second, table[pindex][j+1].second);
            //cout << table[index][j].second << " ";
            table[index][j].second=max(min(table[pindex][j].first, table[pindex][j+1].first), table[index][j].second);
            //cout << table[index][j].second << endl;
            curr=table[index][j].second-i;
            if(curr>maxi)
            {
                maxi=curr;
            }
        }
    }

    cout << maxi;

    return 0;
}
