#include <bits/stdc++.h>
const int MAXN = 200005;
int N, A[MAXN];
namespace FastIO {
template <typename T>
inline void read(T& x) {
  x = 0;
  char ch = getchar();
  bool f = false;
  for (; ch < '0' || ch > '9'; f |= (ch == '-'), ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9';
       x = (x << 3) + (x << 1) + (ch ^ '0'), ch = getchar())
    ;
  if (f) x = -x;
}
template <typename T>
inline void write(T& x) {
  if (x == 0) return (void)(putchar('0'));
  if (x < 0) x = -x, putchar('-');
  int arr[20], len = 0;
  for (; x; arr[len++] = x % 10, x /= 10)
    ;
  while (len) putchar(arr[--len] ^ '0');
}
template <typename T>
inline void writeln(T& x) {
  write(x), putchar('\n');
}
}  // namespace FastIO
int main() {
  using FastIO::read;
  read(N);
  int i;
  for (i = 0; i < N; i++) read(A[i]);
  std::sort(A, A + N);
  int ans = 0, lb, j, k, index;
  for (i = 1, j = 1; i < N; i++)
    if (A[i] != A[i - 1]) A[j++] = A[i];
  N = j;
  int max = A[N - 1];
  for (i = N - 2; i >= 0; i--) {
    lb = i;
    for (j = 1; lb < N && A[i] * j <= max; lb = index + 1, j = A[lb] / A[i]) {
      index = std::upper_bound(A + lb, A + N, (k = A[i] * (j + 1) - 1)) - A - 1;
      ans = std::max(ans, A[index] % A[i]);
    }
  }
  FastIO::writeln(ans);
  return 0;
}
