#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 23;
const int MOD = 1e9 + 7;
const int N = 2e5 + 100;
const int A = 2e6 + 100;
vector<int> v;
int prv[A];
bool in[A];
int main() {
  ios_base::sync_with_stdio(0);
  int n, x;
  cin >> n;
  int mx = 0;
  for (int i = 0; i < (n); ++i) {
    cin >> x;
    if (in[x]) continue;
    in[x] = true;
    v.push_back(x);
    mx = max(mx, x);
  }
  int last = 0;
  for (int i = (1); i < (A); ++i) {
    prv[i] = last;
    if (in[i]) last = i;
  }
  int ans = 0;
  for (int i = 0; i < (((int)(v).size())); ++i) {
    int aj = v[i];
    for (int x = (2 * aj); x < (2 * mx + 1); x += (aj)) {
      int ai = prv[x];
      ans = max(ans, ai % aj);
    }
  }
  cout << ans << "\n";
  return 0;
}
