#include <bits/stdc++.h>
using namespace std;
int lst[1000010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  unordered_set<int> check;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    check.insert(a);
  }
  for (int i = 1; i < 1000010; i++) {
    if (check.count(i))
      lst[i] = i;
    else
      lst[i] = lst[i - 1];
  }
  int ans = 0;
  for (auto &e : check) {
    for (int i = 2 * e; i < 2000000; i += e) {
      ans = max(ans, lst[min(i - 1, 1000000)] % e);
    }
  }
  cout << ans << endl;
}
