#include <bits/stdc++.h>
using namespace std;
const int M = 2000000;
int Log2[M + 100];
int st[21][M + 100];
int N;
int a[200010];
void MM() {
  for (int j = 1; j <= Log2[M]; j++)
    for (int i = 1; i + (1 << j) - 1 <= M; i++)
      st[j][i] = max(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
int get(int x, int y) {
  int k = Log2[y - x + 1];
  return max(st[k][x], st[k][y - (1 << k) + 1]);
}
int main() {
  memset(st, -1, sizeof st);
  Log2[1] = 0;
  for (int i = 2; i <= M; i++) {
    Log2[i] = Log2[i - 1];
    if ((1 << Log2[i] + 1) == i) Log2[i]++;
  }
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &a[i]);
    st[0][a[i]] = a[i];
  }
  MM();
  sort(a + 1, a + 1 + N);
  N = unique(a + 1, a + 1 + N) - a - 1;
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    for (int k = 1; k * a[i] <= M; k++) {
      int x = get(a[i] * (k), (k + 1) * a[i] - 1);
      if (x == -1) continue;
      ans = max(ans, x % a[i]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
