#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, i;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i < n; i++) cin >> a[i];
  sort(a.begin(), a.end());
  int ans = 0, range;
  for (i = 0; i < a.size(); i++) {
    if (i && a[i] == a[i - 1]) continue;
    range = (a[a.size() - 1] / a[i]) + 1;
    for (int j = 2; j <= range; j++) {
      int temp = a[i] * j;
      int p = lower_bound(a.begin(), a.end(), temp) - a.begin();
      if (p > 0) ans = max(ans, a[p - 1] % a[i]);
    }
  }
  cout << ans;
}
