#include <bits/stdc++.h>
using namespace std;
int arr[1000002];
int las[1000002];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int val;
    cin >> val;
    arr[val] = 1;
  }
  int last = 0;
  for (int i = 0; i <= 1000001; i++) {
    las[i] = last;
    if (arr[i]) last = i;
  }
  int ans = 0;
  for (int i = 1; i <= 1000001; i++) {
    if (!arr[i]) continue;
    int to = i + i;
    while (true) {
      int val;
      bool f = 0;
      if (to > 1000000) {
        to = 1000001;
        f = 1;
      }
      int last = las[to];
      if (last > i) {
        ans = max(ans, last % i);
      }
      to += i;
      if (f) break;
    }
  }
  cout << ans << endl;
  return 0;
}
