#include <bits/stdc++.h>
using namespace std;
struct Counter {
  static int k;
  Counter() { k++; }
  ~Counter() { k--; }
};
int Counter::k = 0;
template <typename T>
void pr(const string& name, T t) {
  cout << name << " = " << t << endl;
}
template <typename T, typename... Types>
void pr(const string& names, T t, Types... rest) {
  auto comma_pos = names.find(',');
  cout << names.substr(0, comma_pos) << " = " << t << ", ";
  auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
  pr(string(names, next_name_pos), rest...);
}
long long mod(long long& a, long long b) {
  a %= b;
  if (a < 0) a += b;
}
const long long MOD = 1000000007;
const int RIGHT = 1048576;
const int SIZE = 2100000;
bool OK[SIZE];
int N, T[SIZE];
int query(int l, int r, int n, int a, int b) {
  if (a > r || b < l) return 0;
  if (a >= l && b <= r) return T[n];
  int mid = (a + b) / 2;
  int ql = query(l, r, 2 * n, a, mid);
  int qr = query(l, r, 2 * n + 1, mid + 1, b);
  return max(ql, qr);
}
void update(int p, int v, int n, int a, int b) {
  if (a > p || b < p) return;
  if (a == b) {
    T[n] = p;
    return;
  }
  int mid = (a + b) / 2;
  update(p, v, 2 * n, a, mid);
  update(p, v, 2 * n + 1, mid + 1, b);
  T[n] = max(T[2 * n], T[2 * n + 1]);
}
int main() {
  scanf("%d", &N);
  for (int i = (1); i <= (N); i++) {
    int x;
    scanf("%d", &x);
    OK[x] = true;
    update(x, x, 1, 1, RIGHT);
  }
  int ans = 0;
  for (int i = (1000000); i >= (2); i--) {
    if (!OK[i] || i <= ans + 1) continue;
    int best = 0;
    for (int r = 2 * i - 1; r - i + 1 <= 1000000; r += i) {
      int l = r - i + 1;
      int x = query(l, min(r, RIGHT), 1, 1, RIGHT);
      best = max(best, x % i);
    }
    ans = max(ans, best);
  }
  cout << ans;
}
