#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n;
int val[2000005] = {};
int pre[2000005] = {};
int main() {
  scanf("%d", &n);
  int a;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    val[a] = 1;
  }
  int ans = 0;
  pre[0] = 0;
  for (int i = 1; i < 2000005; i++) {
    pre[i] = pre[i - 1];
    if (val[i - 1]) pre[i] = i - 1;
  }
  for (int i = 1; i < 2000005; i++) {
    if (!val[i]) continue;
    for (int j = i * 2; j < 2000005; j += i) {
      ans = max(ans, pre[j] - (j - i));
    }
  }
  cout << ans;
  return 0;
}
