#include <bits/stdc++.h>
using namespace std;
set<int> visited;
int main() {
  int n;
  cin >> n;
  int values[222222];
  for (int i = 0; i < n; i++) cin >> values[i];
  sort(values, values + n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (visited.count(values[i]) > 0) continue;
    visited.insert(values[i]);
    int curFactor = 2;
    int pos = i;
    while (values[i] * curFactor <= 10e6 && pos < n) {
      pos = lower_bound(values, values + n, values[i] * curFactor) - values;
      ans = max(ans, values[pos - 1] % values[i]);
      curFactor++;
    }
  }
  cout << ans << endl;
  return 0;
}
