#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes(int n, bool prime[]) {
  memset(prime, true, n);
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * 2; i <= n; i += p) prime[i] = false;
    }
  }
}
long long int xorPairCount(long long int arr[], long long int n,
                           long long int x) {
  int result = 0;
  unordered_map<long long int, long long int> m;
  for (long long int i = 0; i < n; i++) {
    long long int curr_xor = x ^ arr[i];
    if (m.find(curr_xor) != m.end()) result += m[curr_xor];
    m[arr[i]]++;
  }
  return result;
}
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int lcm(long long int a, long long int b) {
  return (a * b) / gcd(a, b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n;
  cin >> n;
  vector<long long int> v;
  for (long long int i = 0; i < n; i++) {
    long long int k;
    cin >> k;
    v.push_back(k);
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  long long int max = 0;
  for (long long int i = 0; i < v.size(); i++) {
    vector<long long int>::iterator it;
    long long int c = 2;
    while (1) {
      it = lower_bound(v.begin(), v.end(), c * v[i]);
      long long int k = *(it - 1);
      if (max < (k % v[i]) && (it - v.begin()) > i) max = k % v[i];
      if (c * v[i] >= v[n - 1]) break;
      c++;
    }
  }
  cout << max;
}
