#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1;
const int big = 1e18;
const int block = 800;
const int mod = 1e6;
int n, h;
int a[N];
int y = 0;
struct seg {
  int t[N * 4];
  void upd(int z, int v = 1, int tl = 1, int tr = N - 1) {
    if (z > tr || z < tl) return;
    if (tl == tr) return (void)(t[v] = z);
    int m = tl + tr >> 1;
    upd(z, v * 2, tl, m);
    upd(z, v * 2 + 1, m + 1, tr);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
  int get(int l, int r, int v = 1, int tl = 1, int tr = N - 1) {
    if (l > tr || tl > r || !t[v]) return 0;
    if (l <= tl && tr <= r) return t[v];
    int m = tl + tr >> 1;
    return max(get(l, r, v * 2, tl, m), get(l, r, v * 2 + 1, m + 1, tr));
  }
} rt;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  vector<int> sub;
  for (int i = 0, x; i < n; i++) cin >> x, rt.upd(x), sub.push_back(x);
  sort((sub).begin(), (sub).end());
  sub.erase(unique((sub).begin(), (sub).end()), sub.end());
  int ans = 0;
  for (int i = sub.size() - 1; i >= 0; i--) {
    int u = sub[i];
    if (u <= ans || u == 1) break;
    for (int j = u; j <= sub.back() * 2; j += u)
      ans = max(rt.get(j, j + u - 1) % u, ans);
  }
  cout << ans;
}
