#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long modi) {
  long long res = 1;
  while (b) {
    if (b % 2) {
      res = (res * a) % modi;
    }
    b /= 2;
    a = (a * a) % modi;
  }
  return res;
}
int dirx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int diry[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int ans = 0;
  int n;
  cin >> n;
  int a[n];
  set<int> st;
  int val;
  for (int i = 0; i < n; i++) {
    cin >> val;
    st.insert(val);
  }
  int k = 0;
  for (auto x : st) a[k++] = x;
  n = k;
  if (n == 1) {
    cout << "0";
    return 0;
  }
  int low[2000005] = {0};
  for (int i = 0; i < n; i++) low[a[i]] = 1;
  int cur = 0;
  for (int i = 0; i < 2000005; i++) {
    int temp = cur;
    if (low[i] == 1) temp = i;
    low[i] = cur;
    cur = temp;
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == 1) continue;
    int cur = 2 * a[i];
    while (cur < 2000005) {
      ans = max(ans, (low[cur] % a[i]));
      cur += a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
