#include <bits/stdc++.h>
using namespace std;
int n, a[200000 + 2];
int flag1[2000000 + 2], n_small[2000000 + 1];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  sort(a, a + n);
  int x = 0;
  for (int i = 1; i <= 2e6; i++) {
    if (a[x] > i)
      n_small[i] = 0;
    else {
      while (x < n && a[x] < i) x++;
      x--;
      n_small[i] = a[x];
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (flag1[a[i]]) continue;
    flag1[a[i]] = 1;
    for (int k = 2, j = k * a[i]; j <= 2 * a[n - 1]; k++, j = k * a[i])
      ans = max(n_small[j] % a[i], ans);
  }
  cout << ans;
}
