#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int a[200010], last[1000010];
int n, ans;
inline void scand(int& ret) {
  char c;
  ret = 0;
  while ((c = getchar()) < '0' || c > '9')
    ;
  while (c >= '0' && c <= '9') ret = ret * 10 + (c - '0'), c = getchar();
}
int main() {
  scand(n);
  int M = 0;
  for (int i = 1; i <= n; i++) {
    scand(a[i]);
    last[a[i]] = a[i];
    M = max(a[i], M);
  }
  ++M;
  for (int i = 1; i < M; i++) {
    if (!last[i]) last[i] = last[i - 1];
  }
  sort(a + 1, a + n + 1);
  n = unique(a + 1, a + n + 1) - a - 1;
  for (int i = n - 1; i >= 1; i--) {
    if (a[i] <= ans) break;
    for (int j = a[i] * 2; j < M && j - a[i] <= a[n]; j += a[i]) {
      ans = max(ans, last[j - 1] % a[i]);
    }
    ans = max(ans, last[M - 1] % a[i]);
  }
  printf("%d\n", ans);
  return 0;
}
