#include <bits/stdc++.h>
using namespace std;
template <class t>
inline void read(t &s) {
  s = 0;
  register int f = 1;
  register char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
  s *= f;
  return;
}
int a[200050], n;
int t[1000050];
inline int lb(int x) {
  register int l = 1, r = n, mid, ans = -1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (a[mid] < x)
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return ans;
}
signed main(void) {
  register int maxi = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) read(a[i]), maxi = max(maxi, a[i]), ++t[a[i]];
  sort(a + 1, a + n + 1);
  n = unique(a + 1, a + n + 1) - a - 1;
  register int ans = 0;
  for (int k = 1; k <= maxi; ++k) {
    for (int b = 1; k * b <= maxi; ++b) {
      if (!t[b]) continue;
      register int pos = lb((k + 1) * b);
      if (pos == -1 || a[pos] < k * b) continue;
      ans = max(ans, a[pos] % b);
    }
  }
  cout << ans << endl;
  return 0;
}
