#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
  int N;
  cin >> N;
  vector<int> as(N, 0);
  for (int i = 0; i < N; ++i) cin >> as[i];
  sort(as.begin(), as.end());
  int nodup = 1;
  for (int i = 1; i < N; ++i)
    if (as[i] != as[i - 1]) as[nodup++] = as[i];
  N = nodup;
  int max_v = as[as.size() - 1];
  vector<int> vs(2 * max_v + 1, 0);
  for (int i = 1; i < N; ++i) {
    for (int j = as[i - 1]; j < as[i]; ++j) vs[j] = as[i - 1];
  }
  for (int i = max_v; i <= 2 * max_v; ++i) vs[i] = max_v;
  int res = 0;
  for (int i = 0; i < N; ++i) {
    int m = as[i];
    for (int u = 2 * m; u <= 2 * max_v; u += m) {
      int tmp = vs[u - 1] - (u - m);
      res = max(res, tmp);
    }
  }
  cout << res << endl;
  return 0;
}
