#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
int A[1110000];
int main() {
  memset(A, 0, sizeof A);
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int cur;
    scanf("%d", &cur);
    A[cur] = 1;
  }
  std::vector<int> v;
  for (int i = 1; i <= 1000001; i++) {
    if (A[i]) {
      v.push_back(i);
    }
  }
  v.push_back(100000001);
  int l = v.size();
  int ans = 0;
  for (int i = 0; i < l - 2; i++) {
    int r = 2;
    int x = v[i];
    while (1) {
      std::vector<int>::iterator cur = lower_bound(v.begin(), v.end(), r * x);
      int c = *cur;
      if (r * x <= c) cur--;
      c = *cur;
      ans = max(ans, c % x);
      if (r * c >= v[l - 2]) break;
      r++;
    }
  }
  printf("%d\n", ans);
  return 0;
}
