#include <bits/stdc++.h>
using namespace std;
set<int> myset;
int main() {
  ios ::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> arr;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (i == 0 || myset.find(x) == myset.end())
      arr.push_back(x), myset.insert(x);
  }
  sort(arr.begin(), arr.end());
  int ans = 0;
  n = arr.size();
  for (int i = 0; i < n; i++) {
    for (int j = 2; j < 1000000; j++) {
      int x = arr[i] * j;
      if (arr[i] * (j - 1) > arr[n - 1]) {
        break;
      }
      int y = lower_bound(arr.begin(), arr.end(), x) - arr.begin();
      y--;
      ans = max(ans, arr[y] % arr[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}
