#include <bits/stdc++.h>
using namespace std;
int getInt() {
  int x;
  scanf("%d", &x);
  return x;
}
long long getLongLong() {
  long long x;
  scanf("%I64d", &x);
  return x;
}
double getDouble() {
  double x;
  scanf("%lf", &x);
  return x;
}
char getChar() {
  char x;
  scanf("%c", &x);
  return x;
}
template <typename t>
t abs(t a) {
  if (a >= 0) return a;
  return -a;
}
vector<string> split(const string& s, char c) {
  vector<string> v;
  stringstream second(s);
  string x;
  while (getline(second, x, c)) v.emplace_back(x);
  return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << "  ";
  err(++it, args...);
}
void FastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
const int Max = 1000010;
int ids[Max] = {0};
int n;
vector<int> a;
int main() {
  n = getInt();
  int x;
  for (int i = 0; i < n; i++) {
    x = getInt();
    ids[x] = x;
  }
  for (int i = 1; i < Max; i++) {
    if (ids[i] == i && i > 1) a.push_back(i);
    ids[i] = max(ids[i], ids[i - 1]);
  }
  int ans = 0;
  for (int i = 0; i < a.size(); i++) {
    int up = a[i], prv = up, num;
    for (int j = up + up - 1; j < Max; j += up) {
      ans = max(ans, ids[j] - prv);
      prv += up;
    }
    ans = max(ids[Max - 1] - prv, ans);
  }
  cout << ans << endl;
  return 0;
}
