#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
long long k, n, m, p;
string s1, s2;
int res[100009], a[200009], b[200009];
int posled[2000009];
long long get_i_bit(long long& x, long long& i) {
  return (((long long)1 << i) & x) != 0;
}
class eqw {
 public:
  int l, r, q;
  bool flag;
  int operator<(eqw another) { return l < another.l; }
};
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  int m = 0;
  for (int i = 0; i < n; ++i) {
    if (i > 0 && a[i] == a[i - 1]) {
    } else {
      b[m] = a[i];
      m++;
      posled[a[i]] = 1;
    }
  }
  int cur_posled = 0, temp = 0;
  for (int i = 0; i < 2000001; ++i) {
    if (posled[i] == 1) temp = i;
    posled[i] = cur_posled;
    cur_posled = temp;
  }
  n = m;
  m--;
  int result = 0;
  while (b[m] > 100) {
    int y = 1;
    while ((y - 1) * b[m] < 1000009) {
      if (posled[y * b[m]] > b[m] && posled[y * b[m]] % b[m] > result) {
        result = posled[y * b[m]] % b[m];
      }
      y++;
    }
    m--;
  }
  while (b[m] > result && m > -1) {
    for (int i = m + 1; i < n; ++i) {
      if (b[i] % b[m] > result) {
        result = b[i] % b[m];
      }
    }
    m--;
  }
  cout << result << endl;
  int kol234;
  cin >> kol234;
}
