#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000100;
int n;
int a[MAXN], s[MAXN];
int main() {
  int n, m = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int c;
    scanf("%d", &c);
    m = max(m, c);
    ++a[c];
  }
  for (int i = 1; i <= m; i++) {
    if (a[i])
      s[i] = s[i - 1] + 1;
    else
      s[i] = s[i - 1];
  }
  int ans = 0;
  for (int i = 1; i <= m; ++i) {
    if (a[i] == 0) continue;
    for (int k = i; k <= m;) {
      int std = min(k + i, m + 1);
      if (s[std] - s[k + ans])
        for (int l = k + ans + 1; l < std; ++l) {
          if (a[l]) ans = (l - k);
        }
      k += i;
    }
  }
  cout << ans << endl;
  return 0;
}
