#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 500;
int p[maxn], n, val[maxn * 5][22], maxv = 0;
void init_rmq() {
  for (int j = 1; (1 << j) <= maxv + 2; ++j)
    for (int i = 0; i + (1 << j) - 1 < maxv + 2; ++i)
      val[i][j] = max(val[i][j - 1], val[i + (1 << (j - 1))][j - 1]);
}
int query(int l, int r) {
  int k = 0;
  while ((1 << (k + 1)) <= r - l + 1) k++;
  return max(val[l][k], val[r - (1 << k) + 1][k]);
}
int main(int argc, char *argv[]) {
  memset(val, 0, sizeof(val));
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &p[i]);
    val[p[i]][0] = p[i];
    maxv = max(maxv, p[i]);
  }
  maxv += 5;
  sort(p, p + n);
  init_rmq();
  int c = unique(p, p + n) - p, ans = 0;
  for (int i = 0; i < c; ++i)
    if (p[i] != 1) {
      int L = p[i] + 1, R = 2 * p[i] - 1, flag = 1;
      if (L >= maxv) {
        L = maxv;
        flag = 0;
      }
      if (R >= maxv) {
        R = maxv;
        flag = 0;
      }
      while (flag) {
        int mz = query(L, R);
        if (mz) ans = max(ans, mz % p[i]);
        L += p[i], R += p[i];
        if (L >= maxv) {
          L = maxv;
          flag = 0;
        }
        if (R >= maxv) {
          R = maxv;
          flag = 0;
        }
      }
      ans = max(ans, query(L, R) % p[i]);
    }
  printf("%d\n", ans);
  return 0;
}
