#include <bits/stdc++.h>
using namespace std;
int a[1000003];
int main() {
  int n;
  cin >> n;
  vector<int> opada;
  int i;
  int x;
  int aa[200005];
  int f = 0;
  for (i = 0; i < n; i++) {
    cin >> x;
    if (!a[x]) {
      aa[f] = x;
      f++;
      a[x] = 1;
    }
  }
  aa[f] = 1e9;
  sort(aa, aa + f);
  int maxx = 0;
  int j = 0;
  for (i = 0; i < f; i++) {
    j = 2;
    while (1) {
      int o = lower_bound(aa + i, aa + f, aa[i] * j) - aa - 1;
      j++;
      maxx = max(aa[o] % aa[i], maxx);
      if (o == f - 1) break;
    }
  }
  cout << maxx;
  return 0;
}
