#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  std::ios::sync_with_stdio(false);
  cin >> n;
  long long arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  long long ans = 0, tmp;
  sort(arr, arr + n);
  for (int i = 0; i < n; i++) {
    if (i && arr[i] == arr[i - 1]) continue;
    tmp = arr[i] * 2;
    while (tmp <= arr[n - 1]) {
      int p = lower_bound(arr, arr + n, tmp) - arr;
      if (p) ans = max(ans, arr[p - 1] % arr[i]);
      tmp += arr[i];
    }
    ans = max(ans, arr[n - 1] % arr[i]);
  }
  cout << ans << endl;
}
