#include <bits/stdc++.h>
using namespace std;
namespace Solution_Max {
void read(int &x) {
  char ch = getchar();
  bool f = 0;
  x = 0;
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  if (f) x = -x;
}
void read(long long &x) {
  char ch = getchar();
  bool f = 0;
  x = 0;
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  if (f) x = -x;
}
int num[200001];
int main() {
  int n, ans = 0;
  read(n);
  for (int i = 0; i < n; i++) {
    read(num[i]);
  }
  sort(num, num + n);
  n = unique(num, num + n) - num;
  for (int i = n - 2; i >= 0; i--) {
    if (num[i] <= ans) break;
    for (int j = 2; num[i] * j <= num[n - 1] * 2; j++) {
      int p = lower_bound(num, num + n, num[i] * j) - num - 1;
      if (p <= i) continue;
      if (num[p] < num[i]) continue;
      ans = max(ans, num[p] % num[i]);
    }
  }
  cout << ans << endl;
  return 0;
}
}  // namespace Solution_Max
int main() { return Solution_Max::main(); }
