#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  set<long long> s;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    s.insert(a);
  }
  n = s.size();
  long long arr[n];
  long long c = 0;
  for (long long i : s) arr[c++] = i;
  long long maxi = 0;
  for (long long i = n - 1; i >= 0; i--) {
    if (maxi >= arr[i]) break;
    for (long long j = 2; j * arr[i] <= 2 * arr[n - 1]; j++) {
      long long low = i;
      long long high = n - 1;
      while (low < high) {
        long long mid = (low + high + 1) / 2;
        if (arr[mid] >= j * arr[i])
          high = mid - 1;
        else
          low = mid;
      }
      maxi = max(arr[low] % arr[i], maxi);
    }
  }
  cout << maxi;
}
