#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, INF = 1e9 + 10;
int v[N];
int main() {
  int n, m, ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  sort(v + 1, v + n + 1);
  m = unique(v + 1, v + n + 1) - (v + 1);
  v[m + 1] = INF;
  for (int i = m; i >= 1; i--) {
    if (v[i] <= 1000) {
      for (int j = i + 1; j <= m; j++) ans = max(ans, v[j] % v[i]);
    } else {
      for (int j = 2 * v[i]; j <= v[m] + v[i]; j += v[i])
        ans = max(ans, v[lower_bound(v + i + 1, v + m + 2, j) - v - 1] % v[i]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
