#include <bits/stdc++.h>
using namespace std;
int ni() {
  int n;
  scanf("%d", &n);
  return n;
}
int solve(vector<int> val) {
  sort(val.begin(), val.end());
  int n = (int)val.size();
  vector<int> p(val.back() + 1, -1);
  for (int i = 0; i < n; ++i) {
    p[val[i]] = val[i];
  }
  for (int i = 1; i < (int)p.size(); ++i) {
    if (p[i] == -1) p[i] = p[i - 1];
  }
  int ans = 0;
  for (int i = 2; i <= val.back(); ++i) {
    if (!binary_search(val.begin(), val.end(), i)) continue;
    for (int j = i - 1; j < (int)p.size(); j += i) {
      if (p[j] >= i) ans = max(ans, p[j] % i);
    }
    ans = max(ans, val.back() % i);
  }
  return ans;
}
int bf(vector<int> val) {
  sort(val.begin(), val.end());
  int ans = 0;
  for (int i = 0; i < (int)val.size(); ++i)
    for (int j = i; j < (int)val.size(); ++j) {
      if (ans < val[j] % val[i]) {
        ans = val[j] % val[i];
      }
    }
  return ans;
}
void deb() {
  int x = 15;
  int n = 8;
  for (int i = 0; i < 1e8; ++i) {
    vector<int> val(n);
    for (int j = 0; j < n; ++j) val[j] = 1 + abs(rand() % x);
    int a = solve(val);
    int b = bf(val);
    if (a != b) {
      cout << a << " " << b << endl;
    }
  }
  cout << "OK" << endl;
  exit(0);
}
int main() {
  int n = ni();
  vector<int> val(n);
  for (int i = 0; i < n; i++) {
    val[i] = ni();
  }
  cout << solve(val) << endl;
  return 0;
}
