#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  set<long long> st;
  long long maxx = 0;
  for (long long i = 0; i < n; i++) {
    long long a;
    scanf("%lld", &a);
    st.insert(a);
    maxx = max(maxx, a);
  }
  vector<long long> v;
  for (auto it : st) {
    v.push_back(it);
  }
  long long mx = 0;
  for (long long i = 0; i < v.size(); i++) {
    long long it = v[i];
    for (long long j = 2 * it; j <= 2 * maxx; j += it) {
      long long p = lower_bound(v.begin(), v.end(), j) - v.begin() - 1;
      if (p == -1) {
        continue;
      }
      p = v[p] % it;
      mx = max(p, mx);
    }
  }
  printf("\n");
  cout << mx << endl;
}
