#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
int dd[1000100], n, p[1000100], f[1000100][22], m;
void init() {
  m = 1000000;
  p[0] = 0;
  for (int i = 1; i <= m; i++) {
    if (1 << (p[i - 1] + 1) <= i)
      p[i] = p[i - 1] + 1;
    else
      p[i] = p[i - 1];
  }
  for (int i = 1; i <= m; i++) f[i][0] = i * dd[i];
  for (int j = 1; j <= p[m]; j++) {
    for (int i = 1; i <= m; i++) {
      f[i][j] = f[i][j - 1];
      int k = i + (1 << (j - 1));
      if (k <= m) f[i][j] = max(f[i][j], f[k][j - 1]);
    }
  }
}
int get(int x, int y) {
  int i = p[y - x + 1];
  return max(f[x][i], f[y + 1 - (1 << i)][i]);
}
void solve() {
  int res = 0;
  for (int i = 2; i <= m; i++)
    if (dd[i]) {
      int j = i;
      while (j <= m) {
        int l = j, r = min(m, j + i - 1);
        res = max(res, get(l, r) - l);
        j += i;
      }
    }
  cout << res << endl;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    dd[x] = 1;
  }
  init();
  solve();
  return 0;
}
