#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int a[2000005];
int main() {
  int n, x, ma = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &x), a[x] = x;
    if (x > ma) ma = x;
  }
  ma <<= 1;
  for (int i = 1; i <= ma; i++)
    if (a[i] != i) a[i] = a[i - 1];
  int ans = 0;
  for (int i = 1; i <= ma; i++)
    if (a[i] == i) {
      for (int j = i + i; j <= ma; j += i) ans = max(ans, a[j - 1] - j + i);
    }
  printf("%d\n", ans);
  return 0;
}
