#include <bits/stdc++.h>
using namespace std;
const int Maxn = 2e6 + 14;
int n, a[Maxn], b[Maxn], ans, m;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i], b[a[i]] = a[i], m = max(m, a[i]);
  sort(a, a + n);
  for (int i = 1; i <= 2 * m; i++) {
    if (b[i] == 0) b[i] = b[i - 1];
  }
  for (int i = m - 1; i > ans; i--) {
    if (b[i] == i)
      for (int j = i * 2 - 1; j < 2 * m; j += i) {
        ans = max(ans, b[j] % i);
      }
  }
  cout << ans;
}
