#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using cpl = complex<ld>;
const ld pi = 3.14159265358979323846264338327950288;
const ld tau = 2 * pi;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          ddy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int nx[8] = {1, 2, 2, 1, -1, -2, -2, -1},
          ny[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
const int N = 2000000;
int arr[200020];
bool chk[2000020];
int ptr[2000020];
void Main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    ptr[arr[i]] = arr[i];
  }
  for (int i = 1; i <= N; i++) {
    if (ptr[i] == 0) {
      ptr[i] = ptr[i - 1];
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (chk[arr[i]]) {
      continue;
    }
    chk[arr[i]] = 1;
    for (int j = arr[i] - 1; j <= N; j += arr[i]) {
      if (ptr[j] < arr[i]) {
        continue;
      }
      ans = max(ans, ptr[j] % arr[i]);
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(0);
  Main();
}
