#include <bits/stdc++.h>
using namespace std;
const int N = 3e6 + 5;
int n, x, pre[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> x, pre[x]++;
  for (int i = 1; i < N; i++) pre[i] += pre[i - 1];
  int res = 0;
  for (int i = N - 1; i >= 1; i--) {
    if (pre[i] - pre[i - 1] == 0 || res >= i - 1) continue;
    for (int j = i; j + i - 1 < N; j += i) {
      int l = j, r = j + i - 1, val = pre[r], pos = j - 1;
      while (l <= r) {
        int mid = l + r >> 1;
        if (pre[mid] < val)
          l = mid + 1, pos = mid;
        else
          r = mid - 1;
      }
      res = max(res, pos + 1 - j);
      if (res >= i - 1) break;
    }
  }
  cout << res << '\n';
  return 0;
}
