#include <bits/stdc++.h>
using namespace std;
int n;
int a[2010005];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    a[x] = x;
  }
  for (int i = 1; i < 2000005; i++)
    if (a[i] == 0) a[i] = a[i - 1];
  int ans = 0;
  for (int i = 1; i < 1000001; i++) {
    if (a[i] == i) {
      for (int j = i; j < 1000001; j += i) {
        int k = max(j, a[j + i - 1]);
        ans = max(ans, k - j);
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
