#include <bits/stdc++.h>
using namespace std;
vector<bool> isthere(2000010, false);
vector<int> nextmax(2000010);
int main() {
  ios::sync_with_stdio(false);
  int n, i;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
    isthere[a[i]] = true;
  }
  sort(a.begin(), a.end());
  for (i = 1; i < 2000005; i++) {
    if (isthere[i - 1])
      nextmax[i] = i - 1;
    else
      nextmax[i] = nextmax[i - 1];
  }
  int ans = 0, range;
  for (i = 0; i < a.size(); i++) {
    if (i && a[i] == a[i - 1]) continue;
    range = (a[a.size() - 1] / a[i]) + 1;
    for (int j = 2; j <= range; j++) {
      int temp = a[i] * j;
      ans = max(ans, nextmax[temp] % a[i]);
    }
  }
  cout << ans;
}
