#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
int maxNum = -1;
int n;
bool vis[1000000 + 50];
int main() {
  memset(vis, false, sizeof(vis));
  scanf("%d", &(n));
  for (int i = 0; i < n; i++) {
    int tmp;
    scanf("%d", &(tmp));
    if (!vis[tmp]) {
      arr.push_back(tmp);
      vis[tmp] = true;
      if (tmp > maxNum) {
        maxNum = tmp;
      }
    }
  }
  n = arr.size();
  if (arr.size() == 1) {
    printf("0\n");
    return 0;
  }
  sort(arr.begin(), arr.end());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int num = arr[i];
    int t = 2;
    while (1) {
      int pos = lower_bound(arr.begin(), arr.end(), num * t) - arr.begin() - 1;
      int tmp = arr[pos] % num;
      if (tmp > ans) {
        ans = tmp;
      }
      if (pos == n - 1) {
        break;
      }
      t++;
    }
  }
  printf("%d\n", ans);
  return 0;
}
