#include <bits/stdc++.h>
using namespace std;
int a[1000001], g, j;
int n;
int k = 9999999, k1 = 9999999;
int x = -9999999, x1 = -999999;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > x) {
      x = a[i];
      j = i;
    }
    if (a[i] < k) {
      k = a[i];
      g = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i != g && i != j) {
      x1 = max(x1, a[i]);
      k1 = min(k1, a[i]);
    }
  }
  if (x1 == -999999 || k1 == 9999999)
    cout << 0 << endl;
  else
    cout << min((x1 - k), (x - k1)) << endl;
}
