#include <bits/stdc++.h>
using namespace std;
const int max_n = 3e5;
const int MAX = 3e6 + 10;
vector<int> vec;
set<int> st;
int n, maxi, ans;
int low[MAX + 2];
bool mark[MAX + 2];
int main() {
  scanf("%d", &n);
  vec.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &vec[i]);
    maxi = max(maxi, vec[i]);
    mark[vec[i]] = 1;
  }
  sort(vec.begin(), vec.end());
  vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
  int z = -1;
  for (int i = 0; i < MAX; i++) {
    low[i] = z;
    if (mark[i]) z = i;
  }
  for (int u : vec) {
    int t = u;
    while (t <= maxi) {
      t += u;
      int p = low[t];
      if (p == -1) continue;
      ans = max(ans, u - (t - p));
    }
  }
  printf("%d\n", ans);
  return 0;
}
