#include <bits/stdc++.h>
using namespace std;
int arr[200005];
inline int lowBound(int s, int e, int val) {
  if (val > arr[e]) return arr[e];
  while (s + 1 < e) {
    int mid = (s + e) >> 1;
    ((val <= arr[mid]) ? e : s) = mid;
  }
  return arr[s];
}
int main() {
  int n, maxi, ans = 0, temp, p, val;
  scanf("%d", &(n));
  for (int i = 0; i < n; i++) scanf("%d", &(arr[i]));
  sort(arr, arr + n);
  maxi = arr[n - 1];
  for (int i = 0; i < n; i++) {
    if (i > 0 && arr[i] == arr[i - 1]) continue;
    val = arr[i];
    p = 2;
    val = arr[i] * p;
    while (val <= (arr[i] + maxi)) {
      temp = lowBound(i, n - 1, val);
      temp = temp % arr[i];
      ans = max(ans, temp);
      val = arr[i] * (++p);
    }
  }
  cout << ans << "\n";
}
