#include <bits/stdc++.h>
using namespace std;
const int maxm = 1e6;
int pre[maxm * 2 + 10];
bool a[maxm + 10];
int N;
int main(int argc, const char* argv[]) {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int val;
    scanf("%d", &val);
    pre[val] = -1;
    a[val] = true;
  }
  int small = 0;
  int big = small;
  while (big <= maxm * 2) {
    while (big <= maxm * 2 && pre[big] != -1) {
      big++;
    }
    for (int i = small + 1; i <= big; i++) {
      pre[i] = small;
    }
    small = big;
  }
  int ans = 0;
  for (int i = 1; i <= maxm; i++) {
    if (!a[i]) continue;
    for (int j = i + i; j <= maxm * 2; j += i) {
      ans = max(ans, pre[j] % i);
    }
  }
  printf("%d\n", ans);
  return 0;
}
