#include <bits/stdc++.h>
using namespace std;
signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  set<long long> s;
  for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
       i += 1 - 2 * ((0) > (n))) {
    long long x;
    cin >> x;
    s.insert(x);
  }
  long long a[(long long)s.size()];
  long long p = 0;
  for (long long i : s) a[p++] = i;
  long long ma = 0;
  n = (long long)s.size();
  sort(a, a + n);
  for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
       i += 1 - 2 * ((0) > (n))) {
    long long x = a[i];
    if (x == 1) continue;
    for (long long j = 2 * a[i]; j <= 2 * a[n - 1]; j += a[i]) {
      long long y = lower_bound(a, a + n, j) - a;
      y--;
      ma = max(ma, a[y] % a[i]);
      if (y == (n - 1)) break;
    }
    ma = max(ma, a[n - 1] % a[i]);
  }
  cout << ma << '\n';
}
