#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], b[maxn];
int Search(int l, int r, int x) {
  while (l <= r) {
    int mid = (l + r) / 2;
    if (a[mid] >= x)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return r;
}
int main() {
  int n, x, Max = -1, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    if (i != 0 && a[i] == a[i - 1]) continue;
    int temp = a[i] * 2;
    while (temp <= a[n - 1]) {
      int idx = Search(0, n - 1, temp);
      ans = max(a[idx] % a[i], ans);
      temp += a[i];
    }
    ans = max(ans, a[n - 1] % a[i]);
  }
  printf("%d\n", ans);
  return 0;
}
