#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int n;
int num[N];
int arr[N];
int bf() {
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (num[i] < num[j]) ans = max(ans, num[j] % num[i]);
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", num + i);
    arr[num[i]] = num[i];
  }
  for (int i = 1; i < N; i++) {
    if (arr[i] == 0) arr[i] = arr[i - 1];
  }
  int ans = 0;
  for (int cur = 1; cur < N; cur++) {
    if (arr[cur] == cur)
      for (int j = cur; j + cur - 1 < N; j += cur) {
        int v = arr[j + cur - 1];
        if (v > j && ans < v - j) ans = v - j;
      }
  }
  cout << ans << endl;
  return 0;
}
