#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ar[n], y1, y2;
  int min1 = INT_MAX, max1 = 0, max2, min2, i2, i3;
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  for (int i = 0; i < n; i++) {
    if (max1 <= ar[i]) {
      max1 = ar[i];
      i2 = i;
    }
    if (min1 >= ar[i]) {
      min1 = ar[i];
      i3 = i;
    }
  }
  max2 = 0;
  min2 = INT_MAX;
  for (int i = 0; i < n; i++) {
    if (i != i2 && ar[i] >= max2) max2 = max(ar[i], max2);
    if (i != i3 && ar[i] <= min2) min2 = min(ar[i], min2);
  }
  y1 = max1 - min2;
  y2 = max2 - min1;
  y1 = min(y1, y2);
  if (n == 2) y1 = 0;
  cout << y1 << endl;
  return 0;
}
