#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
const int maxm = 1000006;
int num[maxn];
int G[maxm + 10];
int main() {
  memset(G, 0, sizeof(G));
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    G[num[i]] = num[i];
  }
  sort(num + 1, num + 1 + n);
  for (int i = 1; i <= num[n]; i++)
    if (G[i] != i) G[i] = G[i - 1];
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (num[i] == num[i - 1]) continue;
    for (int j = num[i] + num[i]; j <= num[n]; j += num[i]) {
      ans = max(ans, G[j - 1] % num[i]);
    }
    ans = max(ans, num[n] % num[i]);
  }
  cout << ans << endl;
}
