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