#include <bits/stdc++.h>
using namespace std;
int i, j, k, l, n, m, x, y, cnt, res;
int a[200009];
set<int> st;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    st.insert(a[i]);
  }
  n = 0;
  for (auto to : st) a[++n] = to;
  for (i = 1; i <= n; i++) {
    for (j = a[i] * 2; j <= a[n]; j += a[i]) {
      k = lower_bound(a + 1, a + 1 + n, j) - a - 1;
      if (k > i) res = max(res, a[k] % a[i]);
    }
    res = max(res, a[n] % a[i]);
  }
  cout << res << '\n';
  exit(0);
}
