#include <bits/stdc++.h>
using namespace std;
const int MAX_V = 1000001;
int n;
int a[MAX_V];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int v;
    scanf("%d", &v);
    a[v] = v;
  }
  for (int i = 1; i < MAX_V; i++) {
    if (a[i] == 0) a[i] = a[i - 1];
  }
  int r = 0;
  for (int i = 2; i < MAX_V; i++) {
    if (a[i] == i) {
      for (int j = i; j < MAX_V; j += i) {
        if (a[min(j + i - 1, MAX_V - 1)] - j < i) {
          r = max(r, a[min(j + i - 1, MAX_V - 1)] - j);
        }
      }
    }
  }
  cout << r << endl;
}
