#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
const int maxm = 2000010;
int a[maxn], b[maxm];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  n = int(unique(a, a + n) - a);
  a[n] = maxm;
  int p = a[0] + 1;
  for (int i = 1; i <= n; ++i) {
    while (p <= a[i]) {
      b[p++] = a[i - 1];
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = a[i] << 1; j < maxm; j += a[i]) {
      ans = max(ans, b[j] - j + a[i]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
