#include <bits/stdc++.h>
using namespace std;
int II;
long long I_O;
char CC, SS[20];
const int N = 1e6 + 4;
inline long long read() {
  II = 1, I_O = 0;
  while (!isdigit(CC = getchar()))
    if (CC == '-') II = -1;
  while (isdigit(CC)) I_O = I_O * 10 + CC - '0', CC = getchar();
  return I_O * II;
}
inline void wonl() { putchar('\n'); }
inline void wws() {}
inline void dbg() { cout << endl; }
inline void ww(long long k) {
  if (k < 0) putchar('-'), k *= -1;
  II = 0;
  while (k) SS[++II] = k % 10, k /= 10;
  if (!II) SS[++II] = 0;
  while (II) putchar(SS[II--] + '0');
}
template <typename T, typename... V>
inline void wonl(T t, V... v) {
  ww(t);
  if (sizeof...(v)) putchar(' ');
  wonl(v...);
}
template <typename T, typename... V>
inline void wws(T t, V... v) {
  ww(t);
  putchar(' ');
  wws(v...);
}
template <typename T, typename... V>
inline void dbg(T t, V... v) {
  cout << ' ' << t;
  dbg(v...);
}
bool ar[N];
void solve() {
  int n = read(), a;
  for (int i = 0; i < n; i++) a = read(), ar[a] = 1;
  vector<int> v;
  for (int i = 0; i < N; i++)
    if (ar[i]) v.push_back(i);
  int ma = 2 * (v.back()), ans = 0;
  for (int i = 0; i < v.size(); i++) {
    for (int k = 1; k * v[i] <= ma; k++) {
      int ind = lower_bound(v.begin(), v.end(), k * v[i]) - v.begin();
      ind--;
      if (ind >= 0 && v[ind] >= v[i]) ans = max(ans, (v[ind]) % (v[i]));
    }
  }
  wonl(ans);
}
int main() {
  solve();
  return 0;
}
