#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T a) {
  return ((a < 0) ? -a : a);
}
template <typename T>
inline T sqr(T a) {
  return a * a;
}
const int N = 2000009;
const long long MOD = 1000000007;
const int INF = 1e9;
const long long INF64 = 1e18;
const long double PI = 3.1415926535897932384626433832795;
int n;
vector<int> a;
int last[N];
bool x[N];
bool read() {
  if (!(cin >> n)) return false;
  a.resize(n);
  for (int i = 0; i < (int)(n); ++i) {
    cin >> a[i];
    x[a[i]] = true;
  }
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
  n = a.size();
  return true;
}
void solve() {
  int nlast = -1;
  for (int i = 0; i < (int)(N); ++i) {
    last[i] = nlast;
    if (x[i]) nlast = i;
  }
  int res = 0;
  for (int i = 0; i < (int)(n); ++i) {
    int pos = 2 * a[i], d = a[i];
    for (; pos < N; pos += d) {
      int nres = last[pos] - pos + d;
      res = max(res, nres);
    }
  }
  cout << res;
}
int main() {
  srand(time(NULL));
  cout << setprecision(10) << fixed;
  while (read()) solve();
  return 0;
}
