#include <bits/stdc++.h>
using namespace std;
const int N = 2050000;
const int inf = 2000000;
bool ever[N];
int maxyue[N];
int last[N];
int main() {
  int n, i, j, now;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &now);
    if (!ever[now]) {
      ever[now] = 1;
      for (j = now * 2; j <= inf; j += now) maxyue[j] = max(maxyue[j], now);
    }
  }
  now = 0;
  int ans = 0;
  for (i = 1; i <= inf; i++) {
    if (ever[i - 1]) now = i - 1;
    if (i - now < maxyue[i]) ans = max(ans, maxyue[i] - i + now);
  }
  printf("%d\n", ans);
  return 0;
}
