#include <bits/stdc++.h>
using namespace std;
const long long int N = 5e5 + 5;
void fun() {}
long long int md = 1e9 + 7;
long long int __gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return __gcd(b, a % b);
}
long long int poww(long long int a, long long int b) {
  long long int res = 1;
  while (b) {
    if (b & 1) {
      res *= a;
      res %= md;
    }
    a *= a;
    a %= md;
    b >>= 1;
  }
  return res;
}
void ainp(long long int arr[], long long int n) {
  for (long long int i = 1; i <= n; i++) cin >> arr[i];
}
long long int divide(long long int a, long long int b) {
  long long int rr = a * (poww(b, md - 2));
  rr %= md;
  return rr;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  fun();
  long long int n;
  map<long long int, long long int> mapp;
  cin >> n;
  long long int arr[n + 1];
  ainp(arr, n);
  sort(arr + 1, arr + n + 1);
  long long int maxval = arr[n];
  long long int hashe[maxval * 2 + 1];
  long long int siz = maxval * 2 + 1;
  for (long long int i = 0; i <= n; i++) {
    long long int start = (i == 0 ? 0 : arr[i]);
    long long int end = (i == n ? siz - 1 : arr[i + 1]);
    for (long long int j = start + 1; j <= end; j++) hashe[j] = start;
  }
  long long int maxans = 0;
  for (long long int i = 1; i <= n; i++) {
    if (mapp.count(arr[i])) continue;
    mapp[arr[i]] = 1;
    for (long long int j = arr[i] * 2; j < siz; j += arr[i]) {
      maxans = max(maxans, hashe[j] % arr[i]);
    }
  }
  cout << maxans << "\n";
}
