#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1;
  while (t--) {
    long long int n, i, j, ans = 0;
    cin >> n;
    vector<long long int> a(n + 1);
    for (i = 1; i <= n; i++) cin >> a[i];
    auto it = a.begin();
    sort(a.begin(), a.end());
    a[0] = -1;
    for (i = 1; i <= n; i++) {
      if (a[i] == a[i - 1]) continue;
      for (j = 2 * a[i]; j <= ((long long int)1e6 + a[i]); j += a[i]) {
        it = lower_bound(a.begin(), a.end(), j);
        it--;
        ans = max(ans, *it - j + a[i]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
