#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> seq(n);
  for (int i = 0; i < n; i++) cin >> seq[i];
  sort((seq).begin(), (seq).end());
  int mx = 0;
  map<int, int> vis;
  for (int i = 0; i < n; i++) {
    if (vis[seq[i]]) continue;
    for (int j = 2 * seq[i];; j += seq[i]) {
      vector<int>::iterator it = lower_bound((seq).begin(), (seq).end(), j);
      it--;
      mx = max(mx, *it % seq[i]);
      if (j > (seq)[(int)((seq)).size() - 1]) break;
    }
    vis[seq[i]] = true;
  }
  cout << mx << endl;
  return 0;
}
