#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long gcdf(long long a, long long b) {
  while (a != 0 && b != 0) {
    if (a > b) {
      a %= b;
    } else {
      b %= a;
    }
  }
  return max(a, b);
}
long long power(long long a, long long b, long long m) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int main() {
  boost();
  long long test = 1, i, j, k, l;
  while (test--) {
    long long n;
    cin >> n;
    vector<int> v;
    map<long long, long long> mpp;
    for (i = 0; i < n; i++) {
      long long x;
      cin >> x;
      if (mpp[x]) continue;
      v.push_back(x);
      mpp[x] = 1;
    }
    sort(v.begin(), v.end());
    n = v.size();
    int ans = 0, ma = v[n - 1];
    for (i = 0; i < n; i++) {
      for (j = v[i] + v[i]; j <= ma + v[i]; j += v[i]) {
        int left = i, right = n - 1;
        int cu = -1;
        while (left <= right) {
          int mid = (left + right) / 2;
          if (v[mid] < j) {
            cu = v[mid];
            left = mid + 1;
          } else
            right = mid - 1;
        }
        if (cu == -1) break;
        ans = max(ans, cu % v[i]);
      }
    }
    cout << ans << endl;
  }
}
