#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1000000 + 10;
int arr[maxn], buf[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
    buf[arr[i]] = arr[i];
  }
  sort(arr + 1, arr + n + 1);
  int maxx = arr[n];
  for (int i = 1; i <= maxx; i++) {
    if (buf[i] != i) {
      buf[i] = buf[i - 1];
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (arr[i - 1] == arr[i]) continue;
    for (int j = arr[i] << 1; j <= maxx; j += arr[i]) {
      ans = max(ans, buf[j - 1] % arr[i]);
    }
    ans = max(ans, maxx % arr[i]);
  }
  printf("%d\n", ans);
  return 0;
}
