#include <bits/stdc++.h>
using namespace std;
map<int, bool> visited;
int main() {
  long long n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &v[i]);
  }
  sort(v.begin(), v.end());
  int res = 0;
  visited[1] = 1;
  for (int i = 0; i < v.size(); ++i) {
    if (visited.count(v[i]) == 0) {
      visited[v[i]] = 1;
      int target = v[i] * 2;
      bool enough = false;
      while (!enough) {
        vector<int>::iterator it = upper_bound(v.begin(), v.end(), target - 1);
        if (it == v.end()) {
          enough = true;
        }
        if (it != v.begin()) {
          it--;
          int val = *it;
          if (val > v[i]) res = max(res, val % v[i]);
        }
        target += v[i];
      }
    }
  }
  printf("%d\n", res);
  return 0;
}
