#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int mpow(int base, int exp);
const int N = 1e5 + 4;
int a[N];
set<int> res[1000];
int main() {
  int i, n, j;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  map<int, int> mep;
  vector<int> A;
  for (i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (mep.find(x) == mep.end()) mep[x] = 1, A.push_back(x);
  }
  sort(A.begin(), A.end());
  n = A.size();
  int ans = 0;
  for (i = 0; i < min(1000, n); i++) {
    int u = A[i];
    for (j = i + 1; j < n; j++) ans = max(ans, A[j] % u);
  }
  for (i = 1000; i < n - 1; i++) {
    int u = A[i];
    while (u + A[i] <= A[n - 1]) {
      auto it = lower_bound(A.begin(), A.end(), u + A[i]);
      if (it == A.end()) {
        u += A[i];
        continue;
      } else
        it--;
      ans = max(ans, (*it) % A[i]);
      u += A[i];
    }
    ans = max(ans, A[n - 1] % A[i]);
  }
  cout << ans << endl;
  return 0;
}
int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((long long)result * base) % mod;
    base = ((long long)base * base) % mod;
    exp >>= 1;
  }
  return result;
}
