#include <bits/stdc++.h>
using namespace std;
set<int> st;
int main(int argc, const char* argv[]) {
  int n;
  scanf("%d", &n);
  for (int x, i = 1; i <= n; ++i) {
    scanf("%d", &x);
    st.insert(x);
  }
  int ans = 0;
  for (auto it = prev(st.end());; --it) {
    if (*it <= ans) break;
    for (int k = 1;; ++k) {
      auto pos = st.lower_bound(k * (*it) + ans + 1);
      if (pos == st.end()) break;
      while (pos != st.end()) {
        if (ans <= *pos % *it)
          ans = *pos % *it;
        else
          break;
        pos++;
      }
    }
    if (it == st.begin()) break;
  }
  printf("%d\n", ans);
  return 0;
}
