#include <bits/stdc++.h>
using namespace std;
int nnn, aaa[1000007], ress;
int main() {
  fstream ff;
  cin >> nnn;
  for (int i = 1; i <= nnn; i++) cin >> aaa[i];
  sort(aaa + 1, aaa + nnn + 1);
  for (int i = 1; i < nnn; i++) {
    if (aaa[i] == aaa[i - 1]) continue;
    int kk = aaa[nnn] / aaa[i] + 1;
    for (int j = 1; j <= kk; j++) {
      int x = lower_bound(aaa + i + 1, aaa + nnn + 1, aaa[i] * j) - aaa - 1;
      ress = max(ress, aaa[x] % aaa[i]);
    }
  }
  cout << ress;
}
