#include <bits/stdc++.h>
using namespace std;
bool used[1000006];
vector<int> v;
int main(void) {
  ios::sync_with_stdio(false);
  int i, d, n, ma = 0;
  while (cin >> n) {
    v.resize(n + 1);
    ma = 0;
    memset(used, false, sizeof(used));
    for (i = 0; i < n; i++) {
      cin >> v[i];
      ma = max(ma, v[i]);
    }
    v[n] = 1 << 30;
    sort(v.begin(), v.end());
    vector<int>::iterator it;
    int ans = 0, num;
    for (i = 0; i < n; i++) {
      num = v[i];
      if (used[v[i]]) continue;
      for (d = 2;; d++) {
        it = lower_bound(v.begin(), v.end(), (d * num));
        it--;
        ans = max(ans, (*it) % num);
        if ((d * num) >= ma) break;
      }
      used[v[i]] = true;
    }
    cout << ans << endl;
  }
  return 0;
}
