#include <bits/stdc++.h>
using namespace std;
template <class T>
void printImpl(const vector<T>& coll) {
  copy(coll.begin(), coll.end(), ostream_iterator<T>(cout, " "));
  cout << endl;
}
template <class T, int N>
void printImpl(T (&coll)[N]) {
  copy(coll, coll + N, ostream_iterator<T>(cout, " "));
  cout << endl;
}
template <class Key, class Value>
void printImpl(const map<Key, Value>& data) {
  typename map<Key, Value>::const_iterator it;
  for (it = data.begin(); it != data.end(); ++it) {
    cout << it->first << ":" << it->second << endl;
  }
}
template <class T>
void printImpl(const T& data) {
  cout << data << endl;
}
int main() {
  int N;
  cin >> N;
  vector<int> aa(N);
  for (int i = 0; i < (N); ++i) {
    cin >> aa[i];
  }
  sort(aa.begin(), aa.end());
  vector<int> a;
  a.push_back(aa[0]);
  for (int i = 1; i < N; ++i) {
    if (aa[i] != a.back()) {
      a.push_back(aa[i]);
    }
  }
  N = a.size();
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (N == 2) {
    cout << (a[1] % a[0]) << endl;
    return 0;
  }
  vector<int> ma(2 * a[N - 1] + 1);
  for (int i = 0; i < (N - 1); ++i) {
    for (int t = a[i] + 1; t <= a[i + 1]; ++t) ma[t] = a[i];
  }
  for (int t = a[N - 1] + 1; t <= 2 * a[N - 1]; ++t) ma[t] = a[N - 1];
  int res = 0;
  for (int i = 0; i < (N - 2); ++i) {
    if (a[i] == a[i + 1]) continue;
    if (a[i] < 1000) {
      for (int j = i + 1; j < N; ++j) {
        res = max(res, a[j] % a[i]);
      }
    } else {
      for (int k = 2; k * a[i] <= 2 * a[N - 1]; ++k) {
        int test = ma[k * a[i]];
        { res = max(res, test % a[i]); }
      }
    }
  }
  cout << res << endl;
  return 0;
}
