#include <bits/stdc++.h>
using namespace std;
int n;
int a[200010];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  sort(a + 1, a + 1 + n);
  int maxv = a[n];
  int len = unique(a + 1, a + 1 + n) - a - 1;
  int ans = 0;
  for (int i = 1; i <= len; i++) {
    for (int j = a[i] + a[i]; j < maxv * 2; j += a[i]) {
      int po = lower_bound(a + 1, a + 1 + len, j) - a - 1;
      ans = max(ans, a[po] - j + a[i]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
