#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 1;
int p[maxn];
void solve(int testcase) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = (0); i <= (n - 1); i++) cin >> a[i];
  sort(a.begin(), a.end());
  n = unique(a.begin(), a.end()) - a.begin();
  if (n == 0) {
    cout << "0";
    return;
  }
  int mx = 0, MAX = a[n - 1], MIN = a[0];
  for (int i = (1); i <= (n - 1); i++) p[a[i]] = a[i - 1];
  int x = MAX;
  for (int i = (2 * MAX); i >= (MIN + 1); i--) {
    if (p[i] == 0)
      p[i] = x;
    else
      x = p[i];
  }
  for (int i = (0); i <= (n - 1); i++) {
    for (int y = 2 * a[i]; true; y += a[i]) {
      mx = max(mx, p[y] % a[i]);
      if (y > MAX) break;
    }
  }
  cout << mx;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << setprecision(10);
  clock_t b = clock();
  int t = 1;
  for (int tt = (1); tt <= (t); tt++) solve(tt);
  clock_t e = clock();
  cerr << (double(e - b) / CLOCKS_PER_SEC) << " sec";
  return 0;
}
