#include <bits/stdc++.h>
using namespace std;
const int maxed = 200000 + 10;
int n, p[maxed], d[maxed * 10][20 + 2];
int main() {
  int query(int, int, int);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
  sort(p + 1, p + 1 + n);
  n = unique(p + 1, p + 1 + n) - p - 1;
  for (int i = 1; i <= n; i++) d[p[i]][0] = p[i];
  for (int j = 1; (1 << j) <= 2 * p[n]; j++)
    for (int i = 1; i + (1 << j) - 1 <= 2 * p[n]; i++)
      d[i][j] = max(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);
  int max_ = 0;
  for (int i = 1; i < n; i++) {
    if (p[i] == 1) continue;
    int len = (int)log2(p[i] - 1);
    for (int j = p[i]; j <= p[n]; j += p[i]) {
      int w = query(j, len, p[i]);
      if (w) max_ = max(max_, w - j);
    }
  }
  printf("%d\n", max_);
  return 0;
}
int query(int j, int len, int now) {
  return max(d[j + 1][len], d[j + now - (1 << len)][len]);
}
