#include <bits/stdc++.h>
using namespace std;
const long double pi = acos(-1.0);
const long double eps = 1e-6;
const int INF = 1E9;
const int MAXN = 100500;
int n, ans, maxelem;
vector<int> a;
int main() {
  cin >> n;
  a.resize(n);
  for (int i = 0; i < (int)(n); i++) cin >> a[i];
  sort((a).begin(), (a).end());
  std::vector<int>::iterator it;
  it = std::unique(a.begin(), a.end());
  a.resize(std::distance(a.begin(), it));
  n = a.size();
  maxelem = a[n - 1];
  ans = 0;
  for (int i = 0; i < (int)(n); i++) {
    int k = a[i];
    if (k == 0 || k == 1) continue;
    for (int j = 2 * k; j - k <= maxelem; j += k) {
      int pos = lower_bound((a).begin(), (a).end(), j) - a.begin();
      pos--;
      if (pos < 0 || pos >= n) continue;
      ans = max(ans, a[pos] % k);
    }
  }
  cout << ans;
  return 0;
}
