/**
 * @author Juan Sebastian Beltran Rojas
 * @mail jsbeltran.valhalla@gmail.com
 * @veredict
 * @url
 * @category
 * @date
 **/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.*;

import static java.lang.Integer.parseInt;

public class CF484B {

    static int[] arr;

    public static void main(String args[]) throws Throwable {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader in = new BufferedReader(new FileReader("in.in"));
        long T = System.currentTimeMillis();
        for (String ln; (ln = in.readLine()) != null; ) {
            int N = parseInt(ln);
            StringTokenizer st = new StringTokenizer(in.readLine());
            int[] numbers = new int[N];
            int withOutRepeat = 0;
            int M = 1000001;
            arr = new int[M];
            boolean[] vis = new boolean[M];
            for (int i = 0; i < N; i++) {
                int n = parseInt(st.nextToken());
                if(!vis[n]) {
                    vis[n]=true;
                    numbers[withOutRepeat++]=n;
                }
            }
            Arrays.sort(numbers, 0, withOutRepeat);
            Arrays.fill(arr, -1);

            for(int i=withOutRepeat-2;i>=0;i--)
                for(int j = numbers[i]; j < numbers[i+1];j++)
                    arr[j] = numbers[i+1];

            for(int j=0;j<numbers[0];j++)
                arr[j] = numbers[0];

            int max = 0;

            for (int i=withOutRepeat-1;i>=0;i--)
                max = Math.max(max, f(numbers[i], max));

            System.out.println(max);
        }
    }

    static int f(int T, int max) {
        int N = (int) Math.sqrt(T);
        for (int i = 2; i <= N; i++) {
            int a = arr[T / i];
            if (a != -1) max = Math.max(max, T % a);
        }
        return max;

    }
}


/**
 * @author Juan Sebastian Beltran Rojas
 * @mail jsbeltran.valhalla@gmail.com
 * @veredict
 * @url
 * @category
 * @date
 **/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

import static java.lang.Integer.parseInt;

public class CF484B {

    static int[] arr;

    public static void main(String args[]) throws Throwable {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader in = new BufferedReader(new FileReader("in.in"));
        long T = System.currentTimeMillis();
        for (String ln; (ln = in.readLine()) != null; ) {
            int N = parseInt(ln);
            StringTokenizer st = new StringTokenizer(in.readLine());
            int[] numbers = new int[N];
            int withOutRepeat = 0;
            int M = 1000001;
            arr = new int[M];
            boolean[] vis = new boolean[M];
            for (int i = 0; i < N; i++) {
                int n = parseInt(st.nextToken());
                if (!vis[n]) {
                    vis[n] = true;
                    numbers[withOutRepeat++] = n;
                }
            }
            Arrays.sort(numbers, 0, withOutRepeat);
            Arrays.fill(arr, -1);

            for (int i = withOutRepeat - 2; i >= 0; i--)
                for (int j = numbers[i]; j <= numbers[i + 1]; j++)
                    arr[j] = numbers[i + 1];

            for (int j = 0; j <= numbers[0]; j++)
                arr[j] = numbers[0];

            int max = 0;

            for (int i = withOutRepeat - 1; i >= 0; i--)
                if(numbers[i]>max) {
                    max = Math.max(max, f(numbers[i], max));
                }else break;

            System.out.println(max);
            //System.out.println(System.currentTimeMillis() - T);
        }
    }

    static int f(int T, int max) {
        int N = (int) Math.sqrt(T);
        for (int i = 2; i <= N; i++) {
            if(T/i + 1< max)break;
            for (int j = -1; j < 2; j++) {
                if (T / i + j >= 0) {
                    int a = arr[T / i + j];
                    if (a != -1) max = Math.max(max, T % a);
                }
            }
        }
        return max;

    }
}


/**
 * @author Juan Sebastian Beltran Rojas
 * @mail jsbeltran.valhalla@gmail.com
 * @veredict
 * @url
 * @category
 * @date
 **/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeSet;

import static java.lang.Integer.parseInt;

public class CF484B {
    static TreeSet<Integer> set;

    public static void main(String args[]) throws Throwable {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader in = new BufferedReader(new FileReader("in.in"));
        long T=System.currentTimeMillis();
        for (String ln; (ln = in.readLine()) != null; ) {
            int N = parseInt(ln);
            StringTokenizer st = new StringTokenizer(in.readLine());
            set = new TreeSet<>();
            for (int i = 0; i < N; i++) {
                set.add(parseInt(st.nextToken()));
            }
            int max = 0;
            //int s=0;
            for (int a: set.descendingSet())
                if (a > max) {
                    max = Math.max(max, f(a));
                    //s+=Math.sqrt(a);
                }

            System.out.println(max);
        }
    }

    static int f(int T) {
        int max = 0, N = Math.min((int)Math.ceil(Math.sqrt(T)), 5);
        for (int i = 2; i <= N; i++) {
            for (int j = -1; j < 2; j++) {
                Integer a = set.floor(T / i + j);
                if (a != null && a < T) max = Math.max(max, T % a);
                a = set.ceiling(T / i + j);
                if (a != null && a < T) max = Math.max(max, T % a);
            }
        }
        return max;

    }
}


/**
 * @author Juan Sebastian Beltran Rojas
 * @mail jsbeltran.valhalla@gmail.com
 * @veredict
 * @url
 * @category
 * @date
 **/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeSet;

import static java.lang.Integer.parseInt;

public class CF484B {
    static int[] arr;
    static TreeSet<Integer> set;

    public static void main(String args[]) throws Throwable {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader in = new BufferedReader(new FileReader("in.in"));
        for (String ln; (ln = in.readLine()) != null; ) {
            int N = parseInt(ln);
            arr = new int[N];
            StringTokenizer st = new StringTokenizer(in.readLine());
            set = new TreeSet<>();
            for (int i = 0; i < N; i++) {
                arr[i] = parseInt(st.nextToken());
                set.add(arr[i]);
            }
            Arrays.sort(arr);
            //int bruteForce = 0;
            int max=0;
            for (int i = 0; i < N; i++) {
                //bruteForce = Math.max(bruteForce, bruteForce(i));
                max = Math.max(max, f(arr[i]));
            }
            //if(bruteForce!=max)
            System.out.println(max);
        }
    }

    static int f(int T, int p) {
        return arr[T] % arr[p];
    }

    static int bruteForce(int T) {
        int max = 0;
        for (int i = 0; i < T; i++)
            max = Math.max(max, f(T, i));
        return max;
    }

    static int f(int T) {
        int max = 0;
        for (int i = 2; i <= Math.ceil(Math.sqrt(T)); i++) {
            for (int j = -1; j < 2; j++) {
                Integer a = set.floor(T/i+j);
                if(a!=null&&a<T)max = Math.max(max, T%a);
                a = set.ceiling(T/i+j);
                if(a!=null&&a<T)max = Math.max(max, T%a);
            }
        }
        return max;

    }
}


/**
 * @author Juan Sebastian Beltran Rojas
 * @mail jsbeltran.valhalla@gmail.com
 * @veredict
 * @url
 * @category
 * @date
 **/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeSet;

import static java.lang.Integer.parseInt;

public class CF484B {
    static int[] arr;
    static TreeSet<Integer> set;

    public static void main(String args[]) throws Throwable {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader in = new BufferedReader(new FileReader("in.in"));
        for (String ln; (ln = in.readLine()) != null; ) {
            int N = parseInt(ln);
            arr = new int[N];
            StringTokenizer st = new StringTokenizer(in.readLine());
            set = new TreeSet<>();
            for (int i = 0; i < N; i++) {
                arr[i] = parseInt(st.nextToken());
                set.add(arr[i]);
            }
            Arrays.sort(arr);
            //int bruteForce = 0;
            int max=0;
            for (int i = 0; i < N; i++) {
                //bruteForce = Math.max(bruteForce, bruteForce(i));
                max = Math.max(max, f(arr[i]));
            }
            //if(bruteForce!=max)
            System.out.println(max);
        }
    }

    static int f(int T, int p) {
        return arr[T] % arr[p];
    }

    static int bruteForce(int T) {
        int max = 0;
        for (int i = 0; i < T; i++)
            max = Math.max(max, f(T, i));
        return max;
    }

    static int f(int T) {
        int max = 0;
        for (int i = 1; i <= Math.ceil(Math.sqrt(T))+1; i++) {
            for (int j = -1; j < 2; j++) {
                Integer a = set.floor(T/i+j);
                if(a!=null)max = Math.max(max, T%a);
                a = set.ceiling(T/i+j);
                if(a!=null)max = Math.max(max, T%a);
            }
        }
        return max;

    }
}


import java.io.*;
import java.util.*;
public class c
{
static PrintWriter out = new PrintWriter(System.out);
static int floor(int a[],int val,int n){

         int l=0,r=n;
            while((r-l)>1){
                  int mid = (l+r)/2;
                  if(a[mid]<=val)
                     l=mid;
                  else
                     r=mid;
            }
         return a[l];
}
public static void main(String[] args)
{
    int n = ni();
    int v[] = new int[1000008];
    int a[] = new int[n];
    for(int i=0;i<n;i++)
           v[ni()]=1;

   n=0;
   for(int i=0;i<v.length;i++)
      if(v[i]==1)
          a[n++]=i;

   int ans=0;
   for(int i=0;i<n;i++){
          int temp = 2*a[i];
           int max = (a[n-1]/a[i])*a[i];
               max = max+a[i];
          while(temp<=max){

                  int x = floor(a,temp-1,n);
                  ans = Math.max(ans,x%a[i]);
                  temp=temp+a[i];
           }
    }
         out.println(ans);
         out.flush();

}

static long mod = 1000000007;
static void fact(long a[]){
         a[0]=1;
         for(int i=1;i<a.length;i++)
                a[i] = (i*a[i-1])%mod;
}
static long gcd(long a,long b){

  if(b%a==0) return a;
  return gcd(b%a,a);
}
static long pow(long a,long b,long mod){

long ans=1;
while(b>0){
if(b%2==1)
    ans = (ans*a)%mod;
    b=b/2;
    a=(a*a)%mod;
}
   return ans;
}
    static FastReader sc=new FastReader();

      static int ni(){
                 int x = sc.nextInt();
                 return(x);
        }
      static long nl(){
              long x = sc.nextLong();
              return(x);
         }
      static String n(){
                 String str = sc.next();
                     return(str);
       }
     static String ns(){
                 String str = sc.nextLine();
                   return(str);
      }
     static double nd(){
               double d = sc.nextDouble();
                 return(d);
       }
  static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;
public class c
{
static PrintWriter out = new PrintWriter(System.out);

public static void main(String[] args)
{
    int n = ni();
    int v[] = new int[1000008];
    int f[] = new int[1000008]; // this array to store floor value for each element. using binary search to find floor gives TLE
    int a[] = new int[n];

    for(int i=0;i<n;i++)
           v[ni()]=1;

   n=0;
   int max =0;
   for(int i=1;i<v.length;i++){
      if(v[i]==1)
        {  a[n++]=i; f[i]=max; max=i;}
      else
           f[i]=max;
   }
   int ans=0;
   for(int i=0;i<n;i++){
          int temp = 2*a[i];
           int large = (a[n-1]/a[i])*a[i];
               large = large+a[i];
          while(temp<=large){
                  int x = a[n-1];
                  if(temp<=a[n-1])
                     x = f[temp];
                  ans = Math.max(ans,x%a[i]);
                  temp=temp+a[i];
           }
    }
         out.println(ans);
         out.flush();

}

static long mod = 1000000007;
static void fact(long a[]){
         a[0]=1;
         for(int i=1;i<a.length;i++)
                a[i] = (i*a[i-1])%mod;
}
static long gcd(long a,long b){

  if(b%a==0) return a;
  return gcd(b%a,a);
}
static long pow(long a,long b,long mod){

long ans=1;
while(b>0){
if(b%2==1)
    ans = (ans*a)%mod;
    b=b/2;
    a=(a*a)%mod;
}
   return ans;
}
    static FastReader sc=new FastReader();

      static int ni(){
                 int x = sc.nextInt();
                 return(x);
        }
      static long nl(){
              long x = sc.nextLong();
              return(x);
         }
      static String n(){
                 String str = sc.next();
                     return(str);
       }
     static String ns(){
                 String str = sc.nextLine();
                   return(str);
      }
     static double nd(){
               double d = sc.nextDouble();
                 return(d);
       }
  static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;
public class c
{
static PrintWriter out = new PrintWriter(System.out);
static int floor(int a[],int val){

         int l=0,r=a.length;
            while((r-l)>1){
                  int mid = (l+r)/2;
                  if(a[mid]<=val)
                     l=mid;
                  else
                     r=mid;
            }
         return a[l];
}
public static void main(String[] args)
{
    int n = ni();
    int a[] = new int[n];
    for(int i=0;i<n;i++)
           a[i] = ni();
       Arrays.sort(a);
   int ans=0;
   for(int i=0;i<n;i++){
          if(i>0&&a[i]==a[i-1])
              continue;
          int temp = 2*a[i];
           int max = (a[n-1]/a[i])*a[i];
               max = max+a[i];
          while(temp<=max){

                  int x = floor(a,temp-1);
                  ans = Math.max(ans,x%a[i]);
                  temp=temp+a[i];
           }
    }
         out.println(ans);
         out.flush();

}

static long mod = 1000000007;
static void fact(long a[]){
         a[0]=1;
         for(int i=1;i<a.length;i++)
                a[i] = (i*a[i-1])%mod;
}
static long gcd(long a,long b){

  if(b%a==0) return a;
  return gcd(b%a,a);
}
static long pow(long a,long b,long mod){

long ans=1;
while(b>0){
if(b%2==1)
    ans = (ans*a)%mod;
    b=b/2;
    a=(a*a)%mod;
}
   return ans;
}
    static FastReader sc=new FastReader();

      static int ni(){
                 int x = sc.nextInt();
                 return(x);
        }
      static long nl(){
              long x = sc.nextLong();
              return(x);
         }
      static String n(){
                 String str = sc.next();
                     return(str);
       }
     static String ns(){
                 String str = sc.nextLine();
                   return(str);
      }
     static double nd(){
               double d = sc.nextDouble();
                 return(d);
       }
  static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;
public class c
{
static PrintWriter out = new PrintWriter(System.out);
static int floor(Integer a[],int val){

         int l=0,r=a.length;
            while((r-l)>1){
                  int mid = (l+r)/2;
                  if(a[mid]<=val)
                     l=mid;
                  else
                     r=mid;
            }
         return a[l];
}
public static void main(String[] args)
{
    int n = ni();
    Integer a[] = new Integer[n];
    for(int i=0;i<n;i++)
           a[i] = ni();
       Arrays.sort(a);
   int ans=0;
   for(int i=0;i<n;i++){
          if(i>0&&a[i]==a[i-1])
              continue;
          int temp = 2*a[i];
           int max = (a[n-1]/a[i])*a[i];
               max = max+a[i];
          while(temp<=max){

                  int x = floor(a,temp-1);
                  ans = Math.max(ans,x%a[i]);
                  temp=temp+a[i];
           }
    }
         out.println(ans);
         out.flush();

}

static long mod = 1000000007;
static void fact(long a[]){
         a[0]=1;
         for(int i=1;i<a.length;i++)
                a[i] = (i*a[i-1])%mod;
}
static long gcd(long a,long b){

  if(b%a==0) return a;
  return gcd(b%a,a);
}
static long pow(long a,long b,long mod){

long ans=1;
while(b>0){
if(b%2==1)
    ans = (ans*a)%mod;
    b=b/2;
    a=(a*a)%mod;
}
   return ans;
}
    static FastReader sc=new FastReader();

      static int ni(){
                 int x = sc.nextInt();
                 return(x);
        }
      static long nl(){
              long x = sc.nextLong();
              return(x);
         }
      static String n(){
                 String str = sc.next();
                     return(str);
       }
     static String ns(){
                 String str = sc.nextLine();
                   return(str);
      }
     static double nd(){
               double d = sc.nextDouble();
                 return(d);
       }
  static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;
public class c
{
static PrintWriter out = new PrintWriter(System.out);

public static void main(String[] args)
{
    int n = ni();
    int v[] = new int[1000008];
    int f[] = new int[1000008];
    int a[] = new int[n];

    for(int i=0;i<n;i++)
           v[ni()]=1;

   n=0;
   int max =0;
   for(int i=1;i<v.length;i++){
      if(v[i]==1)
        {  a[n++]=i; f[i]=max; max=i;}
      else
           f[i]=max;
   }
   int ans=0;
   for(int i=0;i<n;i++){
          int temp = 2*a[i];
           int large = (a[n-1]/a[i])*a[i];
               large = large+a[i];
          while(temp<=large){
                  int x = a[n-1];
                  if(temp<=a[n-1])
                     x = f[temp];
                  ans = Math.max(ans,x%a[i]);
                  temp=temp+a[i];
           }
    }
         out.println(ans);
         out.flush();

}

static long mod = 1000000007;
static void fact(long a[]){
         a[0]=1;
         for(int i=1;i<a.length;i++)
                a[i] = (i*a[i-1])%mod;
}
static long gcd(long a,long b){

  if(b%a==0) return a;
  return gcd(b%a,a);
}
static long pow(long a,long b,long mod){

long ans=1;
while(b>0){
if(b%2==1)
    ans = (ans*a)%mod;
    b=b/2;
    a=(a*a)%mod;
}
   return ans;
}
    static FastReader sc=new FastReader();

      static int ni(){
                 int x = sc.nextInt();
                 return(x);
        }
      static long nl(){
              long x = sc.nextLong();
              return(x);
         }
      static String n(){
                 String str = sc.next();
                     return(str);
       }
     static String ns(){
                 String str = sc.nextLine();
                   return(str);
      }
     static double nd(){
               double d = sc.nextDouble();
                 return(d);
       }
  static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;
public class c
{
static PrintWriter out = new PrintWriter(System.out);
static int floor(int a[],int val,int l,int r){

            while((r-l)>1){
                  int mid = (l+r)/2;
                  if(a[mid]<=val)
                     l=mid;
                  else
                     r=mid;
            }
         return a[l];
}
public static void main(String[] args)
{
    int n = ni();
    int v[] = new int[1000008];
    int a[] = new int[n];
    for(int i=0;i<n;i++)
           v[ni()]=1;

   n=0;
   for(int i=0;i<v.length;i++)
      if(v[i]==1)
          a[n++]=i;

   int ans=0;
   for(int i=0;i<n;i++){
          int temp = 2*a[i];
           int max = (a[n-1]/a[i])*a[i];
               max = max+a[i];
          while(temp<=max){

                  int x = floor(a,temp-1,i,n);
                  ans = Math.max(ans,x%a[i]);
                  temp=temp+a[i];
           }
    }
         out.println(ans);
         out.flush();

}

static long mod = 1000000007;
static void fact(long a[]){
         a[0]=1;
         for(int i=1;i<a.length;i++)
                a[i] = (i*a[i-1])%mod;
}
static long gcd(long a,long b){

  if(b%a==0) return a;
  return gcd(b%a,a);
}
static long pow(long a,long b,long mod){

long ans=1;
while(b>0){
if(b%2==1)
    ans = (ans*a)%mod;
    b=b/2;
    a=(a*a)%mod;
}
   return ans;
}
    static FastReader sc=new FastReader();

      static int ni(){
                 int x = sc.nextInt();
                 return(x);
        }
      static long nl(){
              long x = sc.nextLong();
              return(x);
         }
      static String n(){
                 String str = sc.next();
                     return(str);
       }
     static String ns(){
                 String str = sc.nextLine();
                   return(str);
      }
     static double nd(){
               double d = sc.nextDouble();
                 return(d);
       }
  static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Tifuera
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    final int MAX_VAL = (int) 1e6;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        boolean[] isPresent = new boolean[MAX_VAL + 1];
        for (int i = 0; i < n; i++) {
            isPresent[in.nextInt()] = true;
        }

        int[] priv = new int[2 * MAX_VAL + 1];
        for (int i = 1; i <= 2 * MAX_VAL; i++) {
            priv[i] = priv[i - 1];
            if (i <= MAX_VAL && isPresent[i]) {
                priv[i] = i;
            }
        }

        int res = 0;
        for (int i = 1; i <= MAX_VAL; i++) {
            if (!isPresent[i]) {
                continue;
            }
            if(i == 999999){
                int qq = 2;
            }

            for (int j = 2 * i; j <= 2 * MAX_VAL; j += i) {
                int privVal = priv[j - 1];
                res = Math.max(res, privVal - (j - i));
            }
        }
        out.println(res);
    }

}

class InputReader {
    private BufferedReader reader;
    private String[] currentArray;
    private int curPointer;

    public InputReader(InputStream inputStream) {
        reader = new BufferedReader(new InputStreamReader(inputStream));
    }

    public int nextInt() {
        if ((currentArray == null) || (curPointer >= currentArray.length)) {
            try {
                currentArray = reader.readLine().split(" ");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            curPointer = 0;
        }
        return Integer.parseInt(currentArray[curPointer++]);
    }

}


import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Tifuera
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    final int MAX_VAL = (int) 1e6;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        boolean[] isPresent = new boolean[MAX_VAL + 1];
        for (int i = 0; i < n; i++) {
            isPresent[in.nextInt()] = true;
        }

        int[] priv = new int[2 * MAX_VAL + 1];
        for (int i = 1; i <= MAX_VAL; i++) {
            priv[i] = priv[i - 1];
            if (i <= MAX_VAL && isPresent[i]) {
                priv[i] = i;
            }
        }

        int res = 0;
        for (int i = 1; i <= MAX_VAL; i++) {
            if (!isPresent[i]) {
                continue;
            }

            for (int j = 2 * i; j <= 2 * MAX_VAL; j += i) {
                int privVal = priv[j - 1];
                res = Math.max(res, privVal - (j - i));
            }
        }
        out.println(res);
    }

}

class InputReader {
    private BufferedReader reader;
    private String[] currentArray;
    private int curPointer;

    public InputReader(InputStream inputStream) {
        reader = new BufferedReader(new InputStreamReader(inputStream));
    }

    public int nextInt() {
        if ((currentArray == null) || (curPointer >= currentArray.length)) {
            try {
                currentArray = reader.readLine().split(" ");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            curPointer = 0;
        }
        return Integer.parseInt(currentArray[curPointer++]);
    }

}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class CodeforcesPractice {
    Scanner in = new Scanner(System.in);

    private CodeforcesPractice() throws IOException {
        //in.nextLine();
        int n = in.nextInt();
        int[] arr = ns(n);
        int Max = 1000000;
//        int Max = 20;
        boolean[] mat = new boolean[2 * Max + 1];
        for(int i = 0; i < n; i++)
            mat[arr[i]] = true;
        int[] maxLast = new int[2 * Max + 1];
        maxLast[0] = -1;
        for(int i = 1; i < maxLast.length; i++) {
            if(mat[i])
                maxLast[i] = i;
            else
                maxLast[i] = maxLast[i - 1];
        }
        int res = 0;
        for(int i = 0; i < n; i++) {
            int val = arr[i];
            for(int j = 2 * val; j <= 2 * Max; j = j + val) {
                int last = maxLast[j];
                res = Math.max(res, last % val);
            }
        }
        out(res);
    }

    private int[] ns(int n) {
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            res[i] = in.nextInt();
        }
        return res;
    }
    private int[][] getMat(int m, int n) {//m rows and n cols
        int[][] res = new int[m][n];
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                res[i][j] = in.nextInt();
        return res;
    }
    private static void out(Object x) {
        System.out.println(x);
    }
    public static void main(String[] args) throws IOException {
        new CodeforcesPractice();
    }
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class CodeforcesPractice {
    Scanner in = new Scanner(System.in);

    private CodeforcesPractice() throws IOException {
        //in.nextLine();
        int n = in.nextInt();
        int[] arr = ns(n);
        int Max = 1000000;
//        int Max = 20;
        boolean[] mat = new boolean[2 * Max + 1];
        for(int i = 0; i < n; i++)
            mat[arr[i]] = true;
        int[] maxLast = new int[2 * Max + 1];
        maxLast[0] = -1;
        for(int i = 1; i < maxLast.length; i++) {
            if(mat[i])
                maxLast[i] = i;
            else
                maxLast[i] = maxLast[i - 1];
        }
        int res = 0;
        Arrays.sort(arr);
        for(int i = 0; i < n; i++) {
            if(i > 0 && arr[i] == arr[i - 1])
                continue;
            int val = arr[i];
            for(int j = 2 * val; j <= 2 * Max; j = j + val) {
                int last = maxLast[j];
                res = Math.max(res, last % val);
            }
        }
        out(res);
    }

    private int[] ns(int n) {
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            res[i] = in.nextInt();
        }
        return res;
    }
    private int[][] getMat(int m, int n) {//m rows and n cols
        int[][] res = new int[m][n];
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                res[i][j] = in.nextInt();
        return res;
    }
    private static void out(Object x) {
        System.out.println(x);
    }
    public static void main(String[] args) throws IOException {
        new CodeforcesPractice();
    }
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class CodeforcesPractice {
    Scanner in = new Scanner(System.in);

    private CodeforcesPractice() throws IOException {
        //in.nextLine();
        int n = in.nextInt();
        int[] arr = ns(n);
        int Max = 1000000;
//        int Max = 20;
        boolean[] mat = new boolean[2 * Max + 1];
        for(int i = 0; i < n; i++)
            mat[arr[i]] = true;
        int[] maxLast = new int[2 * Max + 1];
        maxLast[0] = -1;
        for(int i = 1; i < maxLast.length; i++) {
            if(mat[i])
                maxLast[i] = i;
            else
                maxLast[i] = maxLast[i - 1];
        }
        int res = 0;
        Arrays.sort(arr);
        for(int i = 0; i < n; i++) {
            if(i > 0 && arr[i] == arr[i - 1])
                continue;
            int val = arr[i];
            for(int j = 2 * val - 1; j <= 2 * Max; j = j + val) {
                int last = maxLast[j];
                res = Math.max(res, last % val);
            }
        }
        out(res);
    }

    private int[] ns(int n) {
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            res[i] = in.nextInt();
        }
        return res;
    }
    private int[][] getMat(int m, int n) {//m rows and n cols
        int[][] res = new int[m][n];
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                res[i][j] = in.nextInt();
        return res;
    }
    private static void out(Object x) {
        System.out.println(x);
    }
    public static void main(String[] args) throws IOException {
        new CodeforcesPractice();
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = scan.nextInt();
        for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            if (num > 1000000) {
                continue;
            }
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size || i < max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            System.out.println(0);
            scan.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                    }
                    offset++;
                }
                loop++;
            }
        }
        System.out.println(maxMod);

        scan.close();
    }

}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 0;
        for (int i = 1; i <= max; i++) {
            if (i > next && preOffset < sortNumberics.size()) {
                pre = sortNumberics.get(preOffset++);
                next = sortNumberics.get(preOffset);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num < maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    // maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre <= limit - num) {
                    continue;
                } else {
                    // maxMod = maxMod > numberics[limit].pre % num ? maxMod :
                    // numberics[limit].pre % num;
                    maxMod = maxMod > numberics[limit].pre - num ? maxMod : numberics[limit].pre - num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                        break;
                    }
                    offset++;
                }
                loop++;
            }
        }
        out.println(maxMod);
        out.close();

    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = scan.nextInt();
        for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            System.out.println(0);
            scan.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                    }
                    offset++;
                }
                loop++;
            }
        }
        System.out.println(maxMod);

        scan.close();
    }

}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int sortOffset = 1;
        for (int i = 1; i <= max; i++) {
            if (i == next) {
                sortOffset++;
            } else if (i > next) {
                pre = sortNumberics.get(sortOffset - 1);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    break;
                } else if (numberics[limit].pre == num) {
                    continue;
                } else {
                    maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 0;
        for (int i = 1; i <= max; i++) {
            if (i > next && preOffset < sortNumberics.size()) {
                pre = sortNumberics.get(preOffset++);
                next = sortNumberics.get(preOffset);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num < maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    // maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre <= limit - num) {
                    continue;
                } else {
//                     maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                    maxMod = maxMod > num + numberics[limit].pre - limit ? maxMod : num + numberics[limit].pre - limit;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 0;
        for (int i = 1; i <= max; i++) {
            if (i > next && preOffset < sortNumberics.size()) {
                pre = sortNumberics.get(preOffset++);
                next = sortNumberics.get(preOffset);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num < maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    // maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre <= limit - num) {
                    continue;
                } else {
                     maxMod = maxMod > numberics[limit].pre % num ? maxMod :
                     numberics[limit].pre % num;
//                    maxMod = maxMod > numberics[limit].pre - num ? maxMod : numberics[limit].pre - num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 0;
        for (int i = 1; i <= max; i++) {
            if (i == next) {
                preOffset++;
            } else if (i > next && preOffset < sortNumberics.size()) {
                next = sortNumberics.get(preOffset);
                pre = sortNumberics.get(preOffset - 1);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num < maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    // maxMod = maxMod > (num + max - limit) ? maxMod : (num +
                    // max - limit);
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre == num) {
                    continue;
                } else {
                    maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 0;
        for (int i = 1; i <= max; i++) {
            if (i > next && preOffset < sortNumberics.size()) {
                pre = sortNumberics.get(preOffset++);
                next = sortNumberics.get(preOffset);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num < maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    // maxMod = maxMod > (num + max - limit) ? maxMod : (num +
                    // max - limit);
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else {
                    maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 0;
        for (int i = 1; i <= max; i++) {
            if (i > next && preOffset < sortNumberics.size()) {
                pre = sortNumberics.get(preOffset++);
                next = sortNumberics.get(preOffset);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num < maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
//                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                     maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre <= limit - num) {
                    continue;
                } else {
                    // maxMod = maxMod > numberics[limit].pre % num ? maxMod :
                    // numberics[limit].pre % num;
                    maxMod = maxMod > numberics[limit].pre - num ? maxMod : numberics[limit].pre - num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 0;
        for (int i = 1; i <= max; i++) {
            if (i > next && preOffset < sortNumberics.size()) {
                pre = sortNumberics.get(preOffset++);
                next = sortNumberics.get(preOffset);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num < maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    maxMod = maxMod > (num + max - limit) ? maxMod : (num + max - limit);
                    // maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre <= limit - num) {
                    continue;
                } else {
//                     maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                    maxMod = maxMod > num + numberics[limit].pre - limit ? maxMod : numberics[limit].pre - limit;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 1;
        for (int i = 1; i <= max; i++) {
            if (i == next) {
                preOffset++;
            } else if (i > next && preOffset < sortNumberics.size()) {
                next = sortNumberics.get(preOffset);
                pre = sortNumberics.get(preOffset - 1);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    // maxMod = maxMod > (num + max - limit) ? maxMod : (num +
                    // max - limit);
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre == num) {
                    continue;
                } else {
                    maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean[] numberics = new boolean[1000000];
        int max = 0;
        // int maxMod = 0;
        int size = scan.nextInt();
        for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size || i < max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            System.out.println(0);
            scan.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                    }
                    offset++;
                }
                loop++;
            }
        }
        System.out.println(maxMod);

        scan.close();
    }

}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean[] numberics = new boolean[1000000];
        int max = 0;
        // int maxMod = 0;
        int size = scan.nextInt();
        for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size || i < max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            System.out.println(sortNumberics.get(0));
            scan.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                    }
                    offset++;
                }
                loop++;
            }
        }
        System.out.println(maxMod);

        scan.close();
    }

}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = scan.nextInt();
        for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i < max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            System.out.println(0);
            scan.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                    }
                    offset++;
                }
                loop++;
            }
        }
        System.out.println(maxMod);

        scan.close();
    }

}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class CopyOfMain {

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        SmartNumberic[] numberics = new SmartNumberic[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int num = in.nextInt();
            max = max > num ? max : num;
            numberics[num] = new SmartNumberic(true);
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            numberics[i] = numberics[i] != null ? numberics[i] : new SmartNumberic();
            if (numberics[i].exist) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            out.println(0);
            out.close();
            return;
        }
        int pre = 0;
        int next = sortNumberics.get(0);
        int preOffset = 1;
        for (int i = 1; i <= max; i++) {
            if (i == next) {
                preOffset++;
            } else if (i > next) {
                next = sortNumberics.get(preOffset);
                pre = sortNumberics.get(preOffset - 1);
            }
            numberics[i].pre = pre;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int limit = num;
            while (true) {
                if ((limit += num) > max) {
                    // 可优化
                    // maxMod = maxMod > (num + max - limit) ? maxMod : (num +
                    // max - limit);
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                } else if (numberics[limit].pre == num) {
                    continue;
                } else {
                    maxMod = maxMod > numberics[limit].pre % num ? maxMod : numberics[limit].pre % num;
                }
            }
        }
        out.println(maxMod);
        out.close();

    }

    static class SmartNumberic {
        boolean exist;
        Integer pre;

        public SmartNumberic() {};

        public SmartNumberic(boolean exist){
            this.exist = exist;
        }
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = scan.nextInt();
        for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size && i <= max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            System.out.println(0);
            scan.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                        break;
                    }
                    offset++;
                }
                loop++;
            }
        }
        System.out.println(maxMod);

        scan.close();
    }

}


/*
 * Copyright 2016 cainiao.com All right reserved. This software is the
 * confidential and proprietary information of cainiao.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with cainiao.com.
 */
package vjudge.MaximumValue;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author qinze
 * @since Aug 1, 2017
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean[] numberics = new boolean[1000001];
        int max = 0;
        // int maxMod = 0;
        int size = scan.nextInt();
        for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            max = max > num ? max : num;
            numberics[num] = true;
        }
        List<Integer> sortNumberics = new ArrayList<Integer>(size - 1);
        for (int i = 1; sortNumberics.size() < size || i < max; i++) {
            if (numberics[i]) {
                sortNumberics.add(i);
            }
        }
        if (sortNumberics.size() == 1) {
            System.out.println(0);
            scan.close();
            return;
        }
        // 初始化最大余数为最大2个数
        int maxMod = sortNumberics.get(sortNumberics.size() - 1) % sortNumberics.get(sortNumberics.size() - 2);
        for (int i = sortNumberics.size() - 3; i > -1; i--) {
            int num = sortNumberics.get(i);
            if (num <= maxMod) {
                // 当前数小于最大余数时，直接跳出
                break;
            }
            int loop = 2, limit = 0;
            while (true) {
                if ((limit = num * loop) > max) {
                    // 可优化
                    maxMod = maxMod > max % num ? maxMod : max % num;
                    break;
                }
                int offset = 1;
                while (num - offset > maxMod) {
                    if (numberics[limit - offset]) {
                        maxMod = num - offset;
                    }
                    offset++;
                }
                loop++;
            }
        }
        System.out.println(maxMod);

        scan.close();
    }

}