#include <bits/stdc++.h>
using namespace std;
static const int maxn = 2e5 + 5;
static const int maxv = 2e6 + 5;
long long N;
long long A[maxn];
long long lo[maxv];
long long lower(long long lo, long long hi, long long val) {
  while (lo < hi) {
    if (A[hi] <= val)
      return A[hi];
    else
      hi--;
    long long mid = (lo + hi) / 2;
    if (A[mid] == val)
      return A[mid];
    else if (A[mid] > val)
      hi = mid - 1;
    else
      lo = mid;
  }
  return A[lo];
}
int main() {
  ios::sync_with_stdio(0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A, A + N);
  auto it = unique(A, A + N);
  N = it - A;
  long long maxelem = A[N - 1];
  int curr = 0;
  for (int i = 0; i < maxv; i++) {
    if (i < A[curr])
      lo[i] = 0;
    else if (curr == N - 1 || A[curr + 1] > i)
      lo[i] = A[curr];
    else {
      curr++;
      i--;
    }
  }
  long long ret = 0;
  for (int j = 0; j < N; j++) {
    long long mult = 2;
    while (mult * A[j] <= 2 * maxelem) {
      long long lb = lo[mult * A[j] - 1];
      ret = max(ret, (lb % A[j]));
      mult++;
    }
  }
  cout << ret << endl;
  return 0;
}
