#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int f[maxn], g[maxn];
int n, x;
int main() {
  static int i, j, maxx;
  maxx = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &f[i]);
    g[f[i]] = f[i];
  }
  sort(f, f + n);
  maxx = f[n - 1];
  for (i = 1; i <= maxx; i++)
    if (!g[i]) g[i] = g[i - 1];
  static int ans;
  ans = 0;
  for (i = 0; i < n; i++) {
    if (i && f[i - 1] == f[i]) continue;
    for (j = f[i] << 1; j <= maxx; j += f[i]) {
      ans = max(ans, g[j - 1] - j + f[i]);
    }
    ans = max(ans, f[n - 1] % f[i]);
  }
  printf("%d\n", ans);
  return 0;
}
