#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    long long int n;
    cin >> n;
    map<long long int, long long int> ma;
    for (long long int i = 1; i <= n; i++) {
      long long int tmp;
      cin >> tmp;
      ma[tmp] = 1;
    }
    vector<long long int> v;
    for (auto i : ma) {
      v.push_back(i.first);
    }
    n = v.size();
    long long int maxi = 0;
    for (auto i : v) {
      for (long long int j = 1; (j - 1) * i <= v.back(); j++) {
        long long int idx =
            lower_bound((v).begin(), (v).end(), j * i) - v.begin();
        idx--;
        idx = max(idx, 0ll);
        if (v[idx] >= i) maxi = max(maxi, v[idx] % i);
      }
    }
    cout << maxi;
  }
  return 0;
}
