#include <bits/stdc++.h>
using namespace std;
int n, max_ost = 0;
vector<int> v;
set<int> s;
void find_rez(int q) {
  int i = 1, temp;
  for (int i = 2; i < v[n - 1] / q + 2; i++) {
    temp = v[lower_bound(v.begin(), v.end(), q * i) - v.begin() - 1] % q;
    if (max_ost < temp) max_ost = temp;
  }
}
void transform_this_shit_into_fucking_vector() {
  v.resize(s.size());
  int i = 0;
  for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
    v[i] = *it;
    i++;
  }
  n = v.size();
}
int main() {
  cin >> n;
  int temp;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    s.insert(temp);
  }
  transform_this_shit_into_fucking_vector();
  int j = n - 2;
  while ((max_ost < v[j] - 1) && (j >= 0)) {
    find_rez(v[j]);
    j--;
  }
  cout << max_ost;
  return 0;
}
