#include <bits/stdc++.h>
using namespace std;
int N, j;
int a[(int)2e6 + 7], ans, last[(int)2e6 + 7];
bool vis[(int)2e6 + 7];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    a[x] = 1;
  }
  int mx = 2e6;
  for (int i = 1; i <= mx; i++) {
    if (a[i])
      last[i] = i;
    else
      last[i] = last[i - 1];
  }
  for (int i = 1; i <= 1e6; i++) {
    if (!a[i]) continue;
    for (int j = i + i; j <= mx; j += i) {
      int idx = last[j - 1];
      if (idx > j - i) ans = max(ans, idx % i);
    }
  }
  cout << ans;
}
