#include <bits/stdc++.h>
using namespace std;
int a[222222];
int counts[1111111];
int last_increase[1111111];
bool done[1111111];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i <= 1000000; i++) {
    counts[i] = 0;
    done[i] = false;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    counts[a[i]]++;
  }
  last_increase[0] = 0;
  for (int i = 1; i <= 1000000; i++) {
    if (counts[i] > 0) {
      last_increase[i] = i;
    } else {
      last_increase[i] = last_increase[i - 1];
    }
  }
  int answer = 0;
  for (int i = 1; i <= n; i++) {
    int x = a[i];
    if (done[x]) continue;
    done[x] = true;
    for (int j = 1; j * x <= 1000000; j++) {
      int L = j * x;
      int H = min(1000000, (j + 1) * x - 1);
      if (last_increase[H] < L) {
        continue;
      }
      answer = max(answer, (last_increase[H]) % x);
    }
  }
  cout << answer << endl;
}
