#include <bits/stdc++.h>
using namespace std;
const int MAX_VALUE = 2 * 1000 * 1000 + 10;
const int MAX_N = 2 * 100 * 1000 + 10;
int Prev[MAX_VALUE], a[MAX_N];
bool flag[MAX_VALUE];
int main() {
  int N, i, j, answer = 0;
  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &a[i]);
    Prev[a[i]] = a[i];
  }
  for (i = 1; i < MAX_VALUE; i++) Prev[i] = Prev[i] ? Prev[i] : Prev[i - 1];
  for (i = 0; i < N; i++)
    if (!flag[a[i]]) {
      flag[a[i]] = true;
      for (j = 2; j * a[i] < MAX_VALUE; j++)
        if (Prev[j * a[i] - 1] > a[i])
          answer = max(answer, Prev[j * a[i] - 1] % a[i]);
    }
  printf("%d", answer);
  return 0;
}
