import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class PracticeMonsterrD {

	public static int bs(Integer[] arr,int value,int second) {
		int start = 0;
		int end = arr.length-1;
		int ans = 0;
		while(start<=end) {
			int mid = (start+end)>>1;
			if(arr[mid]>=value) {
				end = mid-1;
			}else {
				ans = mid;
				start= mid+1;
			}
		}
		return arr[ans]>second?arr[ans]-second:0;
	}

	public static int bs_2(Integer[] arr,int val) {
		int start = 0;
		int end = arr.length-1;
		int ans = 0;
		while(start<=end) {
			int mid = (start+end)>>1;
			if(arr[mid]<=val) {
				ans = arr[mid];
				start = mid+1;
			}else {
				end = mid-1;
			}
		}
		return ans;
	}

	public static void solve() {
		int n=s.nextInt();
		Integer[] arr = new Integer[n];
		for(int i=0;i<n;i++) {
			arr[i]=s.nextInt();
		}
		Arrays.sort(arr);
		int max = (int)2e6+5;
		int[] helper = new int[max];
		int pos = 0;
		for(int i=1;i<max;i++) {
			if(pos<n &&(int)arr[pos]==i) {
				helper[i]=i;
				int current = arr[pos];
				int j= pos+1;
				while(j<n && arr[j]==current) {
					j++;
				}
				pos = j;
			}else {
				helper[i]=helper[i-1];
			}
		}
		int ans = 0;
		int last = arr[n-1];
		for(int i=0;i<n-1;i++) {
			int current = arr[i];
			if(i>0 && arr[i]==arr[i-1])continue;
			for(long j= 2*current;j<=last+current;j+=current) {
				ans = Integer.max(ans,helper[(int)j]%current);
			}
		}
		out.println(ans);
	}

	public static void main(String[] args) {
		out = new PrintWriter(new BufferedOutputStream(System.out));
		s = new FastReader();
		solve();
		out.close();
	}

	public static FastReader s;
	public static PrintWriter out;

	public static class FastReader {
		BufferedReader br;
		StringTokenizer st;

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

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

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

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

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

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

}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class PracticeMonsterrD {

	public static int bs(Integer[] arr,int value,int second) {
		int start = 0;
		int end = arr.length-1;
		int ans = 0;
		while(start<=end) {
			int mid = (start+end)>>1;
			if(arr[mid]>=value) {
				end = mid-1;
			}else {
				ans = mid;
				start= mid+1;
			}
		}
		return arr[ans]>second?arr[ans]-second:0;
	}

	public static int bs_2(Integer[] arr,int val) {
		int start = 0;
		int end = arr.length-1;
		int ans = 0;
		while(start<=end) {
			int mid = (start+end)>>1;
			if(arr[mid]<=val) {
				ans = arr[mid];
				start = mid+1;
			}else {
				end = mid-1;
			}
		}
		return ans;
	}

	public static void solve() {
		int n=s.nextInt();
		Integer[] arr = new Integer[n];
		for(int i=0;i<n;i++) {
			arr[i]=s.nextInt();
		}
		Arrays.sort(arr);
		int max = (int)2e6+5;
		int[] helper = new int[max];
		int pos = 0;
		for(int i=1;i<max;i++) {
			if(pos<n &&(int)arr[pos]==i) {
				helper[i]=i;
				int current = arr[pos];
				int j= pos+1;
				while(j<n && arr[j]==current) {
					j++;
				}
				pos = j;
			}else {
				helper[i]=helper[i-1];
			}
		}
		int ans = 0;
		int last = arr[n-1];
		for(int i=0;i<n;i++) {
			int current = arr[i];
			if(i>0 && arr[i]==arr[i-1])continue;
			for(long j= 2*current;j<last+current;j+=current) {
				ans = Integer.max(ans,helper[(int)j-1]%current);
			}
		}
		out.println(ans);
	}

	public static void main(String[] args) {
		out = new PrintWriter(new BufferedOutputStream(System.out));
		s = new FastReader();
		solve();
		out.close();
	}

	public static FastReader s;
	public static PrintWriter out;

	public static class FastReader {
		BufferedReader br;
		StringTokenizer st;

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

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

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

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

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

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

}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class PracticeMonsterrD {

	public static int bs(Integer[] arr,int value,int second) {
		int start = 0;
		int end = arr.length-1;
		int ans = 0;
		while(start<=end) {
			int mid = (start+end)>>1;
			if(arr[mid]>=value) {
				end = mid-1;
			}else {
				ans = mid;
				start= mid+1;
			}
		}
		return arr[ans]>second?arr[ans]-second:0;
	}

	public static void solve() {
		int n=s.nextInt();
		Integer[] arr = new Integer[n];
		for(int i=0;i<n;i++) {
			arr[i]=s.nextInt();
		}
		Arrays.sort(arr);
		int max = (int)1e6;
		int ans = 0;
		for(int i=0;i<n;i++) {
			int current = arr[i];
			for(long j= 2* current;j<=max;j+=current) {
				ans =Integer.max(ans,bs(arr,(int)j,(int)j-current));
			}
		}
		out.println(ans);
	}

	public static void main(String[] args) {
		out = new PrintWriter(new BufferedOutputStream(System.out));
		s = new FastReader();
		solve();
		out.close();
	}

	public static FastReader s;
	public static PrintWriter out;

	public static class FastReader {
		BufferedReader br;
		StringTokenizer st;

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

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

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

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

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

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

}


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

public class CR276_maximum_value {

    public static void main(String... args) throws IOException {
        MyScanner sc = new MyScanner();

        int n = sc.nextInt();
        int[] v = new int[n];

        int max = v[0];
        for (int i = 0; i < n; i++) {
            v[i] = sc.nextInt();
            max = Math.max(max, v[i]);
        }

        int[] all = new int[max+1];
        Arrays.fill(all, -1);

        Arrays.sort(v);
        for (int i = 0; i < n; i++) {
            if (all[v[i]] == -1) {
                all[v[i]] = i;
            }
        }

        for (int i = 1; i <= max; i++) {
            if (all[i] == -1){
                all[i] = all[i-1];
            }
        }

        int out = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 2 * v[i]; j <= max; j += v[i]) {
                int ind = j-1;
                if (all[ind] != -1 && v[all[ind]] > v[i]){
                    out = Math.max(out, v[all[ind]] % v[i]);
                }
            }
            out = Math.max(out, v[n-1] % v[i]);
        }

        System.out.println(out);
    }

    static class MyScanner {

        BufferedReader br;
        StringTokenizer st;

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

        public String next() throws IOException {
            if (st == null || !st.hasMoreTokens()) {
                String line = br.readLine();
                while (line.isEmpty()) {
                    line = br.readLine();
                }
                st = new StringTokenizer(line);
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            String next = next();
            return Integer.parseInt(next);
        }

        private long nextLong() throws IOException {
            String next = next();
            return Long.parseLong(next);
        }
    }
}


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

public class CR276_maximum_value {

    public static void main(String... args) throws IOException {
        MyScanner sc = new MyScanner();

        int n = sc.nextInt();
        int[] v = new int[n];

        int max = v[0];
        for (int i = 0; i < n; i++) {
            v[i] = sc.nextInt();
            max = Math.max(max, v[i]);
        }

        int[] all = new int[max + 1];
        Arrays.fill(all, -1);

        Arrays.sort(v);
        for (int i = 0; i < n; i++) {
            if (all[v[i]] == -1) {
                all[v[i]] = i;
            }
        }

        for (int i = 1; i <= max; i++) {
            if (all[i] == -1) {
                all[i] = all[i - 1];
            }
        }

        int out = 0;
        for (int i = 2; i <= max; i++) {
            if (all[i] >= 0 && v[all[i]] == i) {
                for (int j = 2 * i; j <= max; j += i) {
                    int ind = j - 1;
                    if (all[ind] != -1 && v[all[ind]] > i) {
                        out = Math.max(out, v[all[ind]] % i);
                    }
                }
                out = Math.max(out, v[n - 1] % i);
            }
        }

        System.out.println(out);
    }

    static class MyScanner {

        BufferedReader br;
        StringTokenizer st;

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

        public String next() throws IOException {
            if (st == null || !st.hasMoreTokens()) {
                String line = br.readLine();
                while (line.isEmpty()) {
                    line = br.readLine();
                }
                st = new StringTokenizer(line);
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            String next = next();
            return Integer.parseInt(next);
        }

        private long nextLong() throws IOException {
            String next = next();
            return Long.parseLong(next);
        }
    }
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[i] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}
				pos = r;
				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);
				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = 0; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[mid] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}

				pos = r;

				//pos = bin_search(array,0,n-1,large);

				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);

				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}

//	static int bin_search(int a[], int l,int r,int val)//二分查找小于val的最大值
//	{
//	    int mid;
//	    while(l<=r){
//	        mid=(l+r)>>1;
//	        if(a[mid]==val) return mid-1;
//	        else if(a[mid]>val) r=mid-1;
//	        else l=mid+1;
//	    }
//	    return r;
//	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[i] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}
				pos = r;
				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);
				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
	//	System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[mid] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}

				pos = r;

				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);

				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = 0; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[i] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}

				pos = r;

				//pos = bin_search(array,0,n-1,large);

				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);

				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}

//	static int bin_search(int a[], int l,int r,int val)//二分查找小于val的最大值
//	{
//	    int mid;
//	    while(l<=r){
//	        mid=(l+r)>>1;
//	        if(a[mid]==val) return mid-1;
//	        else if(a[mid]>val) r=mid-1;
//	        else l=mid+1;
//	    }
//	    return r;
//	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

//				while (l<=r) {
//
//					mid = (l+r)>>1;
//
//					if (array[mid] > large)
//					{
//						r = mid - 1;
//					}
//					else if(array[mid] < large)
//					{
//						l = mid + 1;
//					}
//					else
//					{
//						r = mid-1;
//						break;
//					}
//				}
//
//				pos = r;

				pos = bin_search(array, l, r,large);

				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);

				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}

	static int bin_search(int a[],int l,int r,int val)//二分查找小于val的最大值
	{
	    int mid;
	    while(l<=r){
	        mid=(l+r)>>1;
	        if(a[mid]==val) return mid-1;
	        else if(a[mid]>val) r=mid-1;
	        else l=mid+1;
	    }
	    return r;
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-1; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[i] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}
				pos = r;
				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);
				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[mid] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}

				pos = r;

				pos = bin_search(array, l, r,large);

				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);

				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}

	static int bin_search(int a[],int l,int r,int val)//二分查找小于val的最大值
	{
	    int mid;
	    while(l<=r){
	        mid=(l+r)>>1;
	        if(a[mid]==val) return mid-1;
	        else if(a[mid]>val) r=mid-1;
	        else l=mid+1;
	    }
	    return r;
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200005];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max > array[i]) {
				break;
			}

			if (array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1;
			while(large<array[n-1])
			{
				int pos = 0;

				while (l<=r) {
					mid = (l+r)>>2;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[i] < large)
					{
						l = mid + 1;
					}
					else
					{
						pos = mid;
						break;
					}
				}
				pos = r;
				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);
				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200005];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max > array[i]) {
				break;
			}

			if (array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {
					mid = (l+r)>>2;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[i] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}
				pos = r;
				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);
				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200005];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max > array[i]) {
				break;
			}

			if (array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[i] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}
				pos = r;
				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);
				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
	//	System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i =0; i < n; i++)
		{
//			if (max >= array[i]) {
//				break;
//			}
//
			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[i] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}

				pos = r;

				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);

				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i =0; i < n; i++)
		{
//			if (max >= array[i]) {
//				break;
//			}
//
//			if (array[i] == 0 || array[i] == array[i+1]) {
//				continue;
//			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[i] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}

				pos = r;

				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);

				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		//System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

				while (l<=r) {

					mid = (l+r)>>1;

					if (array[mid] > large)
					{
						r = mid - 1;
					}
					else if(array[mid] < large)
					{
						l = mid + 1;
					}
					else
					{
						r = mid-1;
						break;
					}
				}

				pos = r;

				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);

				large += array[i];

				r = n-1;
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}
}


//package G_MaximumValue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class G_MaximumValue {

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
	//	System.setIn(new FileInputStream("src/G_MaximumValue/input.txt"));
		Scanner sc = new Scanner(System.in);

		int n = 0;
		int array[] = new int[200010];

		n = sc.nextInt();

		for (int i = 0; i < n; i++)
		{
			array[i] = sc.nextInt();
		}

		Arrays.sort(array,0,n);

//		for (int i = 0; i < n; i++)
//		{
//			System.out.println(array[i]);
//		}
		int max = 0;
		int large = 0; int l = 0, r = 0, mid = 0;
		for (int i = n-2; i >= 0; i--)
		{
			if (max >= array[i]) {
				break;
			}

			if (array[i] == 0 || array[i] == array[i+1]) {
				continue;
			}

			large = array[i]<<1; l = i+1; r = n-1;
			while(large<=array[n-1])
			{
				int pos = 0;

//				while (l<=r) {
//
//					mid = (l+r)>>1;
//
//					if (array[mid] > large)
//					{
//						r = mid - 1;
//					}
//					else if(array[i] < large)
//					{
//						l = mid + 1;
//					}
//					else
//					{
//						r = mid-1;
//						break;
//					}
//				}
//
//				pos = r;

				pos = bin_search(array,0,n-1,large);

				max = (max>(array[pos]%array[i]))?max:(array[pos]%array[i]);

				large += array[i];
			}

			max = (max>(array[n-1]%array[i]))?max:(array[n-1]%array[i]);
		}

		System.out.println(max);
	}

	static int bin_search(int a[], int l,int r,int val)//二分查找小于val的最大值
	{
	    int mid;
	    while(l<=r){
	        mid=(l+r)>>1;
	        if(a[mid]==val) return mid-1;
	        else if(a[mid]>val) r=mid-1;
	        else l=mid+1;
	    }
	    return r;
	}
}


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class D276
{

    public static void main(String args[])
    {
        FasterScanner s=new FasterScanner();
        int h[]=new int[2*1000010];

        boolean b[]=new boolean[2*1000010];
        int n=s.nextInt();
        //ArrayList<Integer> z=new ArrayList<Integer>();
        //int a[]=new int[n];
        for(int i=0;i<n;i++)
            {
            int tmp=s.nextInt();
            b[tmp]=true;
            h[tmp]=1;
            }

        for(int i=1;i<2*1000010;i++)
        {
            if(h[i]==0)
                h[i]=h[i-1];
            else
                h[i]=i;
        }
        int max=0;
        for(int i=1;i<1000001;i++)
        {
            if(b[i])
            for(int j=2*i;j<2*1000010;j+=i)
            {
                    max=Math.max(h[j-1]%i,max);
            }
        }

        System.out.println(max);
    }

    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = System.in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        public long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }
}


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

public class Main {

    InputReader ir = new InputReader(System.in);
    PrintWriter out = new PrintWriter(System.out);
    final long kmod = 993960000099397l;
    int size = (int)(1e6 + 1);
    void solve() {
        int n = ir.readInt();
        int arr[] = new int[2*size];
        int pos[] = new int[2*size];
        int ans = 0;
        for(int i = 0; i < n; i++) {
            int temp = ir.readInt();
            arr[temp] = 1;
        }
        for(int i = 1; i < 2*size; i++) {
            if(arr[i - 1] != 0) pos[i] = i - 1;
            else pos[i] = pos[i - 1];
        }

        for(int i = 1; i < 2*size; i++) {
            if(arr[i] == 0) continue;

            for(int j = 2 ; i*j < size; j++) {
                ans = Math.max(ans, pos[i*j] % i);
            }
        }
        out.println(ans);
    }

    void run() {
        solve();
        out.close();
    }
    public static void main(String args[]){
        new Main().run();
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }
    public final int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
    }
    public long readLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public String readString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return isWhitespace(c);
    }
        public double readDouble() {
        return Double.parseDouble(readString());
    }

    public static boolean isWhitespace(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public String next() {
        return readString();
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}


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

public class Main {

    InputReader ir = new InputReader(System.in);
    PrintWriter out = new PrintWriter(System.out);
    final long kmod = 993960000099397l;
    int size = (int)(1e6 + 1);
    void solve() {
        int n = ir.readInt();
        int arr[] = new int[2*size];
        int pos[] = new int[2*size];
        int ans = 0;
        for(int i = 0; i < n; i++) {
            int temp = ir.readInt();
            arr[temp] = 1;
        }
        for(int i = 1; i < 2*size; i++) {
            if(arr[i - 1] != 0) pos[i] = i - 1;
            else pos[i] = pos[i - 1];
        }
        for(int i = 1; i < 2*size; i++) {
            if(arr[i] == 0) continue;

            for(int j = 2 ; i*j < 2*size; j++) {
                //out.println(i + " " + i*j + (pos[i*j]));
                ans = Math.max(ans, pos[i*j] % i);
            }
        }
        out.println(ans);
    }

    void run() {
        solve();
        out.close();
    }
    public static void main(String args[]){
        new Main().run();
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }
    public final int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
    }
    public long readLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public String readString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return isWhitespace(c);
    }
        public double readDouble() {
        return Double.parseDouble(readString());
    }

    public static boolean isWhitespace(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public String next() {
        return readString();
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}


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

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);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskD {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        final int MAX = 2 * (int) 1e6;
        boolean[] mark = new boolean[MAX + 1];
        int[] lower = new int[MAX + 1];
        int n = in.nextInt();
        for (int i = 0; i < n; ++i) {
            mark[in.nextInt()] = true;
        }
        int last = 0;
        for (int i = 1; i <= MAX; ++i) {
            lower[i] = last;
            if (mark[i]) {
                last = i;
            }
        }
        int res = 0;
        for (int i = 1; i <= MAX; ++i) {
            if (!mark[i])
                continue;
            for (int j = 2 * i; j <= MAX; j += i) {
                res = Math.max(res, lower[j] % i);
            }
        }
        out.println(res);
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

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

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


import java.util.Arrays;
import java.util.Scanner;

public class CF276D {
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int[] a = new int[n];

        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        Arrays.sort(a);

        int max = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            int p = a[a.length - 1] / a[i] + 1;
            for (int j = 2; j <= p; j++) {
                int multiple = j * a[i];
                int foundIdx = Arrays.binarySearch(a, multiple);
                if (foundIdx < 0) {
                    foundIdx = Math.abs(foundIdx + 1);
                }

                --foundIdx;
                max = Math.max(max, a[foundIdx] % a[i]);
            }
        }

        System.out.println(max);
    }
}


import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class CF276D {
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        Set<Integer> input = new HashSet<Integer>();

        for (int i = 0; i < n; i++) {
            input.add(sc.nextInt());
        }

        Integer[] a = input.toArray(new Integer[0]);
        Arrays.sort(a);

        int max = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; i++) {
            int p = a[a.length - 1] / a[i] + 1;
            for (int j = 2; j <= p; j++) {
                int multiple = j * a[i];
                int foundIdx = Arrays.binarySearch(a, multiple);
                if (foundIdx < 0) {
                    foundIdx = Math.abs(foundIdx + 1);
                }

                --foundIdx;

                int mod = a[foundIdx] % a[i];
                max = Math.max(max, mod);
                /*if (mod == a[i] - 1) {
                 *  break;
                 *}*/
            }
        }

        System.out.println(max);
    }
}


import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class CF276D {
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        Set<Integer> input = new HashSet<Integer>();

        for (int i = 0; i < n; i++) {
            input.add(sc.nextInt());
        }

        Integer[] a = input.toArray(new Integer[0]);
        Arrays.sort(a);

        int max = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; i++) {
            int p = a[a.length - 1] / a[i] + 1;
            for (int j = 2; j <= p; j++) {
                int multiple = j * a[i];
                int foundIdx = Arrays.binarySearch(a, multiple);
                if (foundIdx < 0) {
                    foundIdx = Math.abs(foundIdx + 1);
                }

                --foundIdx;
                max = Math.max(max, a[foundIdx] % a[i]);
            }
        }

        System.out.println(max);
    }
}


import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class CF276D {
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        Set<Integer> input = new HashSet<Integer>();

        for (int i = 0; i < n; i++) {
            input.add(sc.nextInt());
        }

        Integer[] a = input.toArray(new Integer[0]);
        Arrays.sort(a);

        int max = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            int p = a[a.length - 1] / a[i] + 1;
            for (int j = 2; j <= p; j++) {
                int multiple = j * a[i];
                int foundIdx = Arrays.binarySearch(a, multiple);
                if (foundIdx < 0) {
                    foundIdx = Math.abs(foundIdx + 1);
                }

                --foundIdx;
                max = Math.max(max, a[foundIdx] % a[i]);
            }
        }

        System.out.println(max);
    }
}


import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class CF276D {
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        Set<Integer> input = new HashSet<Integer>();

        for (int i = 0; i < n; i++) {
            input.add(sc.nextInt());
        }

        Integer[] a = input.toArray(new Integer[0]);
        Arrays.sort(a);

        int max = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; i++) {
            int p = a[a.length - 1] / a[i] + 1;
            for (int j = 2; j <= p; j++) {
                int multiple = j * a[i];
                int foundIdx = Arrays.binarySearch(a, multiple);
                if (foundIdx < 0) {
                    foundIdx = Math.abs(foundIdx + 1);
                }

                --foundIdx;

                int mod = a[foundIdx] % a[i];
                max = Math.max(max, mod);
                if (mod == a[i] - 1) {
                    break;
                }
            }
        }

        System.out.println(max);
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class CF276D {
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] a = new int[n];

        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        Arrays.sort(a);

        int max = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; i++) {

            if (i != 0 && a[i] == a[i - 1]) {
                continue;
            }

            int border = a[a.length - 1] + a[i];
            for (int multiple = 2 * a[i]; multiple <= border; multiple += a[i]) {
                int foundIdx = Arrays.binarySearch(a, i + 1, a.length, multiple);
                if (foundIdx < 0) {
                    foundIdx = Math.abs(foundIdx + 1);
                }

                --foundIdx;

                int mod = a[foundIdx] % a[i];
                max = Math.max(max, mod);
                /*if (mod == a[i] - 1) {
                 *  break;
                 *}*/
            }
        }

        System.out.println(max);
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class CF276D {
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] a = new int[n];

        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        Arrays.sort(a);

        int max = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; i++) {

            if (i != 0 && a[i] == a[i - 1]) {
                continue;
            }

            int border = a[a.length - 1] + a[i];
            for (int multiple = 2 * a[i]; multiple <= border; multiple += a[i]) {
                int foundIdx = Arrays.binarySearch(a, i + 1, a.length, multiple);
                if (foundIdx < 0) {
                    foundIdx = Math.abs(foundIdx + 1);
                }

                --foundIdx;

                int mod = a[foundIdx] % a[i];
                max = Math.max(max, mod);
                if (mod == a[i] - 1) {
                    break;
                }
            }
        }

        System.out.println(max);
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#define N 1000010
using namespace std;
int a[N],n, b[N],c[N],d[N];

int main()
{
    int mx=0;
    cin >> n;
    for( int i = 0; i < n; i ++ )
    {
        scanf("%d",&a[i]);
        b[a[i]]=1;
        mx=max(mx,a[i]);
    }

    int t=0;

    for( int i = t+1; i <=mx; i ++ ){
        c[i]=t;
        if( b[i] ) t=i;
    }
    t=mx;
    for( int i = mx; i >=0; i -- ){
        d[i]=t;
        if( b[i] ) t=i;
    }

    int ans = 0;
    for( int i = mx; i>0; i -- )
    {
        if(b[i]==0)continue;
        if(0==c[i] )continue;
        if( ans >= i ) break;
        int t = d[i/2];
        while( t<i ){
            //cout<<ans<<endl;
            ans=max(ans,i%t);
            if( ans >= t ) break;
            t=d[t];
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#define N 1000010
using namespace std;
int a[N],n, b[N],c[N];

int main()
{
    int mx=0;
    cin >> n;
    for( int i = 0; i < n; i ++ )
    {
        scanf("%d",&a[i]);
        b[a[i]]=1;
        mx=max(mx,a[i]);
    }

    int t=0;

    for( int i = t+1; i <=mx; i ++ ){
        c[i]=t;
        if( b[i] ) t=i;
    }

    int ans = 0;
    for( int i = 0; i<=mx; i ++ )
    {
        if(b[i]==0)continue;
        if( ans >= i-1 ) continue;
        int t;
        for( int j = 1; j*i<=mx; j++ ){
            t=j*i;
            if( c[t]<=i ) continue;
            ans=max(ans,c[t]%i);
            if( ans >= i-1 )
                break;
        }
        ans=max(ans,mx%i);
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#define N 100010
using namespace std;
int a[N],n;
int get_ans(){
    int ans = 0,t;
    for( int i = n-1; i >=0; i -- ){
        if( ans >= a[i] ) return ans;
        for( int j=i-1; j >=0; j -- ){
            t = a[i]%a[j];
            ans=max(ans,t);
            if( ans>=a[j] )
                break;

        }
    }
    return ans;
}
int main()
{
    cin >> n;
    for( int i = 0; i < n; i ++ )
        scanf("%d",&a[i]);
    sort(a,a+n);
    int j = 1;
    for( int i = 1; i < n; i ++ ){
        if( a[i]!=a[i-1]){
            a[j++]=a[i];
        }
    }
    n=j;
    cout<<get_ans()<<endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#define N 1000010
using namespace std;
int a[N],n, b[N],c[N];

int main()
{
    int mx=0;
    cin >> n;
    for( int i = 0; i < n; i ++ )
    {
        scanf("%d",&a[i]);
        b[a[i]]=1;
        mx=max(mx,a[i]);
    }

    int t=0;

    for( int i = t+1; i <=mx; i ++ ){
        c[i]=t;
        if( b[i] ) t=i;
    }

    int ans = 0;
    for( int i = mx; i>0; i -- )
    {
        if(b[i]==0)continue;
        if(0==c[i] )continue;
        if( ans >= i ) break;
        int t = i;
        while( c[t] > ans ){
            //cout<<ans<<endl;
            ans=max(ans,i%c[t]);
            if( ans >= c[t]) break;
            t=c[t];
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#define N 1000010
using namespace std;
int a[N],n, b[N],c[N],d[N];

int main()
{
    int mx=0;
    cin >> n;
    for( int i = 0; i < n; i ++ )
    {
        scanf("%d",&a[i]);
        b[a[i]]=1;
        mx=max(mx,a[i]);
    }

    int t=0;

    for( int i = t+1; i <=mx; i ++ ){
        c[i]=t;
        if( b[i] ) t=i;
    }
    t=mx;
    for( int i = mx; i >=0; i -- ){
        d[i]=t;
        if( b[i] ) t=i;
    }

    int ans = 0;
    for( int i = 0; i<=mx; i ++ )
    {
        if(b[i]==0)continue;
        if( ans >= i-1 ) break;
        int t;
        for( int j = 1; j*i<mx; j++ ){
            t=j*i;
            if( c[j*i]<=i ) continue;
            ans=max(ans,c[j*i]%i);
            if( ans >= i-1 )
                break;
        }
        b[i]=0;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#define N 1000010
using namespace std;
int a[N],n, b[N],c[N],d[N];

int main()
{
    int mx=0;
    cin >> n;
    for( int i = 0; i < n; i ++ )
    {
        scanf("%d",&a[i]);
        b[a[i]]=1;
        mx=max(mx,a[i]);
    }

    int t=0;

    for( int i = t+1; i <=mx; i ++ ){
        c[i]=t;
        if( b[i] ) t=i;
    }
    t=mx;
    for( int i = mx; i >=0; i -- ){
        d[i]=t;
        if( b[i] ) t=i;
    }

    int ans = 0;
    for( int i = 0; i<=mx; i ++ )
    {
        if(b[i]==0)continue;
        if( ans >= i-1 ) continue;
        int t;
        for( int j = 1; j*i<mx; j++ ){
            t=j*i;
            if( c[j*i]<=i ) continue;
            ans=max(ans,c[j*i]%i);
            if( ans >= i-1 )
                break;
        }
        ans=max(ans,mx%i);
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#define N 1000010
using namespace std;
int a[N],n;
int get_ans(){
    int ans = 0,t;
    for( int i = n-1; i >=0; i -- ){
        if( ans >= a[i] ) return ans;
        for( int j=i-1; j >=0; j -- ){
            t = a[i]%a[j];
            ans=max(ans,t);
            if( ans>=a[j] )
                break;

        }
    }
    return ans;
}
int main()
{
    cin >> n;
    for( int i = 0; i < n; i ++ )
        scanf("%d",&a[i]);
    sort(a,a+n);
    int j = 1;
    for( int i = 1; i < n; i ++ ){
        if( a[i]!=a[i-1]){
            a[j++]=a[i];
        }
    }
    n=j;
    cout<<get_ans()<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define REP(i,s,t) for(int (i)=(s);(i)<=(t);++(i))
#define UREP(i,s,t) for(int (i)=(s);(i)>=(t);--(i))
#define INF 0x7FFFFFFF
typedef long long LL;
typedef pair<int, int> pairi;
typedef vector<int> vi;
const int mod = 1000000007;
const int maxn = 200000;

int arr[maxn+5];

int main() {
    int n, _max = -INF;
    cin >> n;
    REP(i, 0, n-1) {cin >> arr[i];_max = max(_max, arr[i]);}
    sort(arr, arr+n);
    int ans = 0;
    REP(i, 0, n-1) {
        int low = 2*arr[i], up = _max*2;
        for (int j=low;j<=up;j+=arr[i]) {
            int * p = lower_bound(arr, arr+n, j);
            ans = max(ans, (*(p-1))%arr[i]);
        }
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define REP(i,s,t) for(int (i)=(s);(i)<=(t);++(i))
#define UREP(i,s,t) for(int (i)=(s);(i)>=(t);--(i))
#define INF 0x7FFFFFFF
typedef long long LL;
typedef pair<int, int> pairi;
typedef vector<int> vi;
const int mod = 1000000007;
const int maxn = 200000;

int arr[maxn+5];

int main() {
    //freopen("input.in", "r", stdin);
    int n, _max = -INF;
    cin >> n;
    REP(i, 0, n-1) {cin >> arr[i];_max = max(_max, arr[i]);}
    sort(arr, arr+n);
    int ans = 0;
    REP(i, 0, n-1) {
        if (arr[i]-1 < ans) continue;
        if (arr[i]==1) continue;
        int low = 2*arr[i], up = _max*2;
        int * p = arr;
        for (int j=low;j<=up;j+=arr[i]) {
            p = lower_bound(p, arr+n, j);
            ans = max(ans, (*(p-1))%arr[i]);
        }
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define REP(i,s,t) for(int (i)=(s);(i)<=(t);++(i))
#define UREP(i,s,t) for(int (i)=(s);(i)>=(t);--(i))
#define INF 0x7FFFFFFF
typedef long long LL;
typedef pair<int, int> pairi;
typedef vector<int> vi;
const int mod = 1000000007;
const int maxn = 200000;

int arr[maxn+5];

int main() {
    //freopen("input.in", "r", stdin);
    int n;
    scanf("%d",&n);
    REP(i, 0, n-1) scanf("%d",&arr[i]);
    sort(arr, arr+n);
    int ans = 0, _max = arr[n-1];
    REP(i, 0, n-1) {
        if (arr[i]-1 < ans) continue;
        int low = 2*arr[i], up = _max+arr[i];
        int * p = arr;
        for (int j=low;j<=up;j+=arr[i]) {
            p = lower_bound(p, arr+n, j);
            ans = max(ans, (*(p-1))%arr[i]);
        }
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define REP(i,s,t) for(int (i)=(s);(i)<=(t);++(i))
#define UREP(i,s,t) for(int (i)=(s);(i)>=(t);--(i))
#define INF 0x7FFFFFFF
typedef long long LL;
typedef pair<int, int> pairi;
typedef vector<int> vi;
const int mod = 1000000007;
const int maxn = 200000;

int arr[maxn+5];

int main() {
   // freopen("input.in", "r", stdin);
    int n;
    scanf("%d",&n);
    REP(i, 0, n-1) scanf("%d",&arr[i]);
    sort(arr, arr+n);
    int ans = 0, _max = arr[n-1];
    REP(i, 0, n-1) {
        if (arr[i]-1 < ans) continue;
        if (arr[i] == 1) continue;
        if (i>1 && arr[i]==arr[i-1]) continue;
        int low = 2*arr[i], up = _max+arr[i];
        int * p = arr;
        for (int j=low;j<=up;j+=arr[i]) {
            p = lower_bound(p, arr+n, j);
            ans = max(ans, (*(p-1))%arr[i]);
        }
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define REP(i,s,t) for(int (i)=(s);(i)<=(t);++(i))
#define UREP(i,s,t) for(int (i)=(s);(i)>=(t);--(i))
#define INF 0x7FFFFFFF
typedef long long LL;
typedef pair<int, int> pairi;
typedef vector<int> vi;
const int mod = 1000000007;
const int maxn = 200000;

int arr[maxn+5];

int main() {
    int n;
    scanf("%d",&n);
    REP(i, 0, n-1) scanf("%d",&arr[i]);
    sort(arr, arr+n);
    int ans = 0, _max = arr[n-1];
    REP(i, 0, n-1) {
        if (arr[i]-1 < ans) continue;
        if (arr[i] == 1) continue;
        if (i>1 && a[i]==a[i-1]) continue;
        int low = 2*arr[i], up = _max+arr[i];
        int * p = arr;
        for (int j=low;j<=up;j+=arr[i]) {
            p = lower_bound(p, arr+n, j);
            ans = max(ans, (*(p-1))%arr[i]);
        }
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define REP(i,s,t) for(int (i)=(s);(i)<=(t);++(i))
#define UREP(i,s,t) for(int (i)=(s);(i)>=(t);--(i))
#define INF 0x7FFFFFFF
typedef long long LL;
typedef pair<int, int> pairi;
typedef vector<int> vi;
const int mod = 1000000007;
const int maxn = 200000;

int arr[maxn+5];

int main() {
    //freopen("input.in", "r", stdin);
    int n, _max = -INF;
    cin >> n;
    REP(i, 0, n-1) {cin >> arr[i];_max = max(_max, arr[i]);}
    sort(arr, arr+n);
    int ans = 0;
    REP(i, 0, n-1) {
        if (arr[i]-1 < ans) continue;
        int low = 2*arr[i], up = _max*2;
        for (int j=low;j<=up;j+=arr[i]) {
            int * p = lower_bound(arr, arr+n, j);
            ans = max(ans, (*(p-1))%arr[i]);
        }
    }
    cout << ans;
    return 0;
}


/// بسم الله الرحمن الرحيم

/* *********************************************************************
   *                       JU_Undefined_Coders                         *
   *               Team Members: Shaheen, Niloy, B'ru                  *
   ********************************************************************* */

#include<algorithm>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#include<deque>
#include<climits>
#include<complex>

#define S scanf
#define P printf

#define I1(n) scanf("%d",&n)
#define LL1(n) scanf("%lld",&n)
#define I2(n1,n2) scanf("%d%d",&n1,&n2)
#define LL2(n1,n2) scanf("%lld%lld",&n1,&n2)
#define I3(n1,n2,n3) scanf("%d%d%d",&n1,&n2,&n3)
#define LL3(n1,n2,n3) scanf("%lld%lld%lld",&n1,&n2,&n3)

#define  F(i,a,b) for(i=a;i<b; i++)
#define FF(i,a,b) for(i=a;i<=b;i++)
#define FI(i,a,b) for((typeof(b)) i=a;i<=b;i++)
#define FR(i, a, b,inc) for(  i = (a); i <= (b); i+=inc )
#define RFR(i, a, b,dec) for(  i = (a); i >= (b); i-=dec )
#define FS(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define PR(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;

#define LL long long int
#define ULL unsigned long long int
#define D double

#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define _Max(a,b,c) Max(a,Max(b,c))
#define _Min(a,b,c) Min(a,Min(b,c))
#define SQR(n) (n*n)
#define PI 2.0*acos(0.0) //#define PI acos(-1.0)

#define all(a) a.begin(),a.end()
#define X first
#define Y second

#define PCC cout<<"Case "<<cas++<<": ";
#define PC printf("Case %d:",cas++);
#define PCN printf("Case %d:\n",cas++);
#define NL printf("\n");
#define cp printf("here\n");

#define SET(a,i) memset(a,i,sizeof a)
#define MEM(a,val) memset(a,val,sizeof(a))
#define open freopen("input.txt","r",stdin)
#define close freopen ("output.txt","w",stdout)
#define Case(a) printf("Case %d:",a)
#define caseh(a) printf("Case #%d: ",a)
#define S1(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL1(a) scanf("%lld",&a)
#define SL2(a,b) scanf("%lld%lld",&a,&b)
#define SL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define CHR getchar()
#define PB(x) push_back(x)
#define pb push_back
#define PP pop_back()
#define PF(x) push_front(x)
#define PPF(x) pop_front()
#define PS(x) push(x)
#define sz() size()
#define UB upper_bound
#define LB lower_bound
#define mxe(a,n) (*max_element(a,a+n))
#define mne(a,n) (*min_element(a,a+n))
#define SUM(a,n) (accumulate(a,a+n,0))
#define onbits  __builtin_popcount
#define VI vector<int>
#define DBG(n) P("Done %d\n",n);

using namespace std;

template <typename T> T BigMod (T b,T p,T m){if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;}
template <typename T> T ModInv (T b,T m){return BigMod(b,m-2,m);}
template <typename T> void ia (T a[],int n){for (int i=0; i<n; i++) cin >> a[i];}
template <typename T> void pa (T a[],int n){for (int i=0; i<n-1; i++) cout << a[i] << " ";cout << a[n-1] << endl;}
template <typename T> LL isLeft(T a,T b,T c) { return (a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y); }
template <typename T> T in(){char ch;T n = 0;bool ng = false;while (1){ch = getchar();if (ch == '-'){ng = true;ch = getchar();break;}if (ch>='0' && ch<='9')     break;}while (1){n = n*10 + (ch - '0');ch = getchar();if (ch<'0' || ch>'9')   break;}return (ng?-n:n);}
template<typename T>inline T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
template<typename T>inline T Bigmod(T b,T p,T m){ if(p==0) return 1; else if (!(p&1)) return SQR(Bigmod(b,p/2,m)) % m;else return ((b % m) * Bigmod(b,p-1,m)) % m;}
template<typename T>inline T Dis(T x1,T y1,T x2, T y2){return sqrt( SQR(x1-x2) + SQR(y1-y2) );}
template<typename T>inline T Angle(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
template<typename T>inline T DIFF(T a,T b) { T d = a-b;if(d<0)return -d;else return d;}
template<typename T>inline T ABS(T a) {if(a<0)return -a;else return a;}
template<typename T>inline T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<typename T>inline T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<typename T>inline T euclide(T a,T b,T &x,T &y) {if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}   if(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}   if(b==0){x=1;y=0;return a;}else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}}

int Set(int N,int pos){ return N=N | (1<<pos);}
int Reset(int N,int pos){return N= N & ~(1<<pos);}
bool Check(int N,int pos){return (bool)(N & (1<<pos));}
int toInt(string s)  { int sm; stringstream ss(s); ss>>sm; return sm; }
int toLlint(string s) { long long int sm; stringstream ss(s); ss>>sm; return sm;}
int cdigittoint(char ch){return ch-'0';}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
bool isConst(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}

/* ------------------------------------------------------------------------------------------------------- */
//          0123456789
#define MX  100007
#define MOD 1000000007
#define INF (1<<30)-1+(1<<30)
#define eps 1e-9

int a[MX];

int main()
{
    int n;

    S1(n);

    for (int i=1;i<=n;i++)      S1(a[i]);

    sort(a+1,a+1+n);        a[0] = 0;a[n+1] = INF;

    int lim = 10*MX;
    int mx = 0;

    for (int i=1;i<=n;i++){
        for (int j=i+i;j<=lim;j+=i){
            int pos = LB(a+1,a+1+n,j)-a-1;

            if (a[pos] < i)     continue;

//            cout << a[pos] << endl;

            mx = max( a[pos]%i,mx );
        }
    }

    cout << mx << endl;

return 0;
}


/// بسم الله الرحمن الرحيم

/* *********************************************************************
   *                       JU_Undefined_Coders                         *
   *               Team Members: Shaheen, Niloy, B'ru                  *
   ********************************************************************* */

#include<algorithm>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#include<deque>
#include<climits>
#include<complex>

#define S scanf
#define P printf

#define I1(n) scanf("%d",&n)
#define LL1(n) scanf("%lld",&n)
#define I2(n1,n2) scanf("%d%d",&n1,&n2)
#define LL2(n1,n2) scanf("%lld%lld",&n1,&n2)
#define I3(n1,n2,n3) scanf("%d%d%d",&n1,&n2,&n3)
#define LL3(n1,n2,n3) scanf("%lld%lld%lld",&n1,&n2,&n3)

#define  F(i,a,b) for(i=a;i<b; i++)
#define FF(i,a,b) for(i=a;i<=b;i++)
#define FI(i,a,b) for((typeof(b)) i=a;i<=b;i++)
#define FR(i, a, b,inc) for(  i = (a); i <= (b); i+=inc )
#define RFR(i, a, b,dec) for(  i = (a); i >= (b); i-=dec )
#define FS(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define PR(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;

#define LL long long int
#define ULL unsigned long long int
#define D double

#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define _Max(a,b,c) Max(a,Max(b,c))
#define _Min(a,b,c) Min(a,Min(b,c))
#define SQR(n) (n*n)
#define PI 2.0*acos(0.0) //#define PI acos(-1.0)

#define all(a) a.begin(),a.end()
#define X first
#define Y second

#define PCC cout<<"Case "<<cas++<<": ";
#define PC printf("Case %d:",cas++);
#define PCN printf("Case %d:\n",cas++);
#define NL printf("\n");
#define cp printf("here\n");

#define SET(a,i) memset(a,i,sizeof a)
#define MEM(a,val) memset(a,val,sizeof(a))
#define open freopen("input.txt","r",stdin)
#define close freopen ("output.txt","w",stdout)
#define Case(a) printf("Case %d:",a)
#define caseh(a) printf("Case #%d: ",a)
#define S1(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL1(a) scanf("%lld",&a)
#define SL2(a,b) scanf("%lld%lld",&a,&b)
#define SL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define CHR getchar()
#define PB(x) push_back(x)
#define pb push_back
#define PP pop_back()
#define PF(x) push_front(x)
#define PPF(x) pop_front()
#define PS(x) push(x)
#define sz() size()
#define UB upper_bound
#define LB lower_bound
#define mxe(a,n) (*max_element(a,a+n))
#define mne(a,n) (*min_element(a,a+n))
#define SUM(a,n) (accumulate(a,a+n,0))
#define onbits  __builtin_popcount
#define VI vector<int>
#define DBG(n) P("Done %d\n",n);

using namespace std;

template <typename T> T BigMod (T b,T p,T m){if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;}
template <typename T> T ModInv (T b,T m){return BigMod(b,m-2,m);}
template <typename T> void ia (T a[],int n){for (int i=0; i<n; i++) cin >> a[i];}
template <typename T> void pa (T a[],int n){for (int i=0; i<n-1; i++) cout << a[i] << " ";cout << a[n-1] << endl;}
template <typename T> LL isLeft(T a,T b,T c) { return (a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y); }
template <typename T> T in(){char ch;T n = 0;bool ng = false;while (1){ch = getchar();if (ch == '-'){ng = true;ch = getchar();break;}if (ch>='0' && ch<='9')     break;}while (1){n = n*10 + (ch - '0');ch = getchar();if (ch<'0' || ch>'9')   break;}return (ng?-n:n);}
template<typename T>inline T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
template<typename T>inline T Bigmod(T b,T p,T m){ if(p==0) return 1; else if (!(p&1)) return SQR(Bigmod(b,p/2,m)) % m;else return ((b % m) * Bigmod(b,p-1,m)) % m;}
template<typename T>inline T Dis(T x1,T y1,T x2, T y2){return sqrt( SQR(x1-x2) + SQR(y1-y2) );}
template<typename T>inline T Angle(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
template<typename T>inline T DIFF(T a,T b) { T d = a-b;if(d<0)return -d;else return d;}
template<typename T>inline T ABS(T a) {if(a<0)return -a;else return a;}
template<typename T>inline T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<typename T>inline T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<typename T>inline T euclide(T a,T b,T &x,T &y) {if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}   if(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}   if(b==0){x=1;y=0;return a;}else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}}

int Set(int N,int pos){ return N=N | (1<<pos);}
int Reset(int N,int pos){return N= N & ~(1<<pos);}
bool Check(int N,int pos){return (bool)(N & (1<<pos));}
int toInt(string s)  { int sm; stringstream ss(s); ss>>sm; return sm; }
int toLlint(string s) { long long int sm; stringstream ss(s); ss>>sm; return sm;}
int cdigittoint(char ch){return ch-'0';}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
bool isConst(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}

/* ------------------------------------------------------------------------------------------------------- */
//          0123456789
#define MX  100007
#define MOD 1000000007
#define INF (1<<30)-1+(1<<30)
#define eps 1e-9

int a[MX];

int main()
{
    int n;

    S1(n);

    for (int i=1;i<=n;i++)      S1(a[i]);

    sort(a+1,a+1+n);        a[0] = 0;a[n+1] = INF;

    int lim = 10*MX;
    int mx = 0;

    for (int i=1;i<=n;i++){
        for (int j=i+i;j<=lim;j+=i){
            int pos = LB(a+1,a+1+n,j)-a-1;

            if (a[pos] <= i)     continue;

            mx = max( a[pos]%a[i],mx );

            if (pos == n)       break;
        }
    }

    cout << mx << endl;

return 0;
}


/// بسم الله الرحمن الرحيم

/* *********************************************************************
   *                       JU_Undefined_Coders                         *
   *               Team Members: Shaheen, Niloy, B'ru                  *
   ********************************************************************* */

#include<algorithm>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#include<deque>
#include<climits>
#include<complex>

#define S scanf
#define P printf

#define I1(n) scanf("%d",&n)
#define LL1(n) scanf("%lld",&n)
#define I2(n1,n2) scanf("%d%d",&n1,&n2)
#define LL2(n1,n2) scanf("%lld%lld",&n1,&n2)
#define I3(n1,n2,n3) scanf("%d%d%d",&n1,&n2,&n3)
#define LL3(n1,n2,n3) scanf("%lld%lld%lld",&n1,&n2,&n3)

#define  F(i,a,b) for(i=a;i<b; i++)
#define FF(i,a,b) for(i=a;i<=b;i++)
#define FI(i,a,b) for((typeof(b)) i=a;i<=b;i++)
#define FR(i, a, b,inc) for(  i = (a); i <= (b); i+=inc )
#define RFR(i, a, b,dec) for(  i = (a); i >= (b); i-=dec )
#define FS(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define PR(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;

#define LL long long int
#define ULL unsigned long long int
#define D double

#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define _Max(a,b,c) Max(a,Max(b,c))
#define _Min(a,b,c) Min(a,Min(b,c))
#define SQR(n) (n*n)
#define PI 2.0*acos(0.0) //#define PI acos(-1.0)

#define all(a) a.begin(),a.end()
#define X first
#define Y second

#define PCC cout<<"Case "<<cas++<<": ";
#define PC printf("Case %d:",cas++);
#define PCN printf("Case %d:\n",cas++);
#define NL printf("\n");
#define cp printf("here\n");

#define SET(a,i) memset(a,i,sizeof a)
#define MEM(a,val) memset(a,val,sizeof(a))
#define open freopen("input.txt","r",stdin)
#define close freopen ("output.txt","w",stdout)
#define Case(a) printf("Case %d:",a)
#define caseh(a) printf("Case #%d: ",a)
#define S1(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL1(a) scanf("%lld",&a)
#define SL2(a,b) scanf("%lld%lld",&a,&b)
#define SL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define CHR getchar()
#define PB(x) push_back(x)
#define pb push_back
#define PP pop_back()
#define PF(x) push_front(x)
#define PPF(x) pop_front()
#define PS(x) push(x)
#define sz() size()
#define UB upper_bound
#define LB lower_bound
#define mxe(a,n) (*max_element(a,a+n))
#define mne(a,n) (*min_element(a,a+n))
#define SUM(a,n) (accumulate(a,a+n,0))
#define onbits  __builtin_popcount
#define VI vector<int>
#define DBG(n) P("Done %d\n",n);

using namespace std;

template <typename T> T BigMod (T b,T p,T m){if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;}
template <typename T> T ModInv (T b,T m){return BigMod(b,m-2,m);}
template <typename T> void ia (T a[],int n){for (int i=0; i<n; i++) cin >> a[i];}
template <typename T> void pa (T a[],int n){for (int i=0; i<n-1; i++) cout << a[i] << " ";cout << a[n-1] << endl;}
template <typename T> LL isLeft(T a,T b,T c) { return (a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y); }
template <typename T> T in(){char ch;T n = 0;bool ng = false;while (1){ch = getchar();if (ch == '-'){ng = true;ch = getchar();break;}if (ch>='0' && ch<='9')     break;}while (1){n = n*10 + (ch - '0');ch = getchar();if (ch<'0' || ch>'9')   break;}return (ng?-n:n);}
template<typename T>inline T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
template<typename T>inline T Bigmod(T b,T p,T m){ if(p==0) return 1; else if (!(p&1)) return SQR(Bigmod(b,p/2,m)) % m;else return ((b % m) * Bigmod(b,p-1,m)) % m;}
template<typename T>inline T Dis(T x1,T y1,T x2, T y2){return sqrt( SQR(x1-x2) + SQR(y1-y2) );}
template<typename T>inline T Angle(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
template<typename T>inline T DIFF(T a,T b) { T d = a-b;if(d<0)return -d;else return d;}
template<typename T>inline T ABS(T a) {if(a<0)return -a;else return a;}
template<typename T>inline T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<typename T>inline T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<typename T>inline T euclide(T a,T b,T &x,T &y) {if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}   if(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}   if(b==0){x=1;y=0;return a;}else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}}

int Set(int N,int pos){ return N=N | (1<<pos);}
int Reset(int N,int pos){return N= N & ~(1<<pos);}
bool Check(int N,int pos){return (bool)(N & (1<<pos));}
int toInt(string s)  { int sm; stringstream ss(s); ss>>sm; return sm; }
int toLlint(string s) { long long int sm; stringstream ss(s); ss>>sm; return sm;}
int cdigittoint(char ch){return ch-'0';}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
bool isConst(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}

/* ------------------------------------------------------------------------------------------------------- */
//          0123456789
#define MX  200007
#define MOD 1000000007
#define INF (1<<30)-1+(1<<30)
#define eps 1e-9

int a[MX];

int main()
{
    int n;

    S1(n);

    for (int i=1;i<=n;i++)      S1(a[i]);

    sort(a+1,a+1+n);        a[0] = 0;a[n+1] = INF;

    int lim = 10*MX;
    int mx = 0;

    for (int i=1;i<=n;i++){
        for (int j=i+i;j<=lim;j+=i){
            int pos = LB(a+1,a+1+n,j)-a-1;

            if (a[pos] <= a[i])     continue;

            mx = max( a[pos]%a[i],mx );

            if (pos == n)       break;
        }
    }

    cout << mx << endl;

return 0;
}


/// بسم الله الرحمن الرحيم

/* *********************************************************************
   *                       JU_Undefined_Coders                         *
   *               Team Members: Shaheen, Niloy, B'ru                  *
   ********************************************************************* */

#include<algorithm>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#include<deque>
#include<climits>
#include<complex>

#define S scanf
#define P printf

#define I1(n) scanf("%d",&n)
#define LL1(n) scanf("%lld",&n)
#define I2(n1,n2) scanf("%d%d",&n1,&n2)
#define LL2(n1,n2) scanf("%lld%lld",&n1,&n2)
#define I3(n1,n2,n3) scanf("%d%d%d",&n1,&n2,&n3)
#define LL3(n1,n2,n3) scanf("%lld%lld%lld",&n1,&n2,&n3)

#define  F(i,a,b) for(i=a;i<b; i++)
#define FF(i,a,b) for(i=a;i<=b;i++)
#define FI(i,a,b) for((typeof(b)) i=a;i<=b;i++)
#define FR(i, a, b,inc) for(  i = (a); i <= (b); i+=inc )
#define RFR(i, a, b,dec) for(  i = (a); i >= (b); i-=dec )
#define FS(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define PR(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;

#define LL long long int
#define ULL unsigned long long int
#define D double

#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define _Max(a,b,c) Max(a,Max(b,c))
#define _Min(a,b,c) Min(a,Min(b,c))
#define SQR(n) (n*n)
#define PI 2.0*acos(0.0) //#define PI acos(-1.0)

#define all(a) a.begin(),a.end()
#define X first
#define Y second

#define PCC cout<<"Case "<<cas++<<": ";
#define PC printf("Case %d:",cas++);
#define PCN printf("Case %d:\n",cas++);
#define NL printf("\n");
#define cp printf("here\n");

#define SET(a,i) memset(a,i,sizeof a)
#define MEM(a,val) memset(a,val,sizeof(a))
#define open freopen("input.txt","r",stdin)
#define close freopen ("output.txt","w",stdout)
#define Case(a) printf("Case %d:",a)
#define caseh(a) printf("Case #%d: ",a)
#define S1(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL1(a) scanf("%lld",&a)
#define SL2(a,b) scanf("%lld%lld",&a,&b)
#define SL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define CHR getchar()
#define PB(x) push_back(x)
#define pb push_back
#define PP pop_back()
#define PF(x) push_front(x)
#define PPF(x) pop_front()
#define PS(x) push(x)
#define sz() size()
#define UB upper_bound
#define LB lower_bound
#define mxe(a,n) (*max_element(a,a+n))
#define mne(a,n) (*min_element(a,a+n))
#define SUM(a,n) (accumulate(a,a+n,0))
#define onbits  __builtin_popcount
#define VI vector<int>
#define DBG(n) P("Done %d\n",n);

using namespace std;

template <typename T> T BigMod (T b,T p,T m){if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;}
template <typename T> T ModInv (T b,T m){return BigMod(b,m-2,m);}
template <typename T> void ia (T a[],int n){for (int i=0; i<n; i++) cin >> a[i];}
template <typename T> void pa (T a[],int n){for (int i=0; i<n-1; i++) cout << a[i] << " ";cout << a[n-1] << endl;}
template <typename T> LL isLeft(T a,T b,T c) { return (a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y); }
template <typename T> T in(){char ch;T n = 0;bool ng = false;while (1){ch = getchar();if (ch == '-'){ng = true;ch = getchar();break;}if (ch>='0' && ch<='9')     break;}while (1){n = n*10 + (ch - '0');ch = getchar();if (ch<'0' || ch>'9')   break;}return (ng?-n:n);}
template<typename T>inline T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
template<typename T>inline T Bigmod(T b,T p,T m){ if(p==0) return 1; else if (!(p&1)) return SQR(Bigmod(b,p/2,m)) % m;else return ((b % m) * Bigmod(b,p-1,m)) % m;}
template<typename T>inline T Dis(T x1,T y1,T x2, T y2){return sqrt( SQR(x1-x2) + SQR(y1-y2) );}
template<typename T>inline T Angle(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
template<typename T>inline T DIFF(T a,T b) { T d = a-b;if(d<0)return -d;else return d;}
template<typename T>inline T ABS(T a) {if(a<0)return -a;else return a;}
template<typename T>inline T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<typename T>inline T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<typename T>inline T euclide(T a,T b,T &x,T &y) {if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}   if(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}   if(b==0){x=1;y=0;return a;}else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}}

int Set(int N,int pos){ return N=N | (1<<pos);}
int Reset(int N,int pos){return N= N & ~(1<<pos);}
bool Check(int N,int pos){return (bool)(N & (1<<pos));}
int toInt(string s)  { int sm; stringstream ss(s); ss>>sm; return sm; }
int toLlint(string s) { long long int sm; stringstream ss(s); ss>>sm; return sm;}
int cdigittoint(char ch){return ch-'0';}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
bool isConst(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}

/* ------------------------------------------------------------------------------------------------------- */
//          0123456789
#define MX  200007
#define MOD 1000000007
#define INF (1<<30)-1+(1<<30)
#define eps 1e-9

int a[MX];
int prev[MX];

int main()
{
    int n;

    S1(n);

    for (int i=1;i<=n;i++)      S1(a[i]);

    sort(a+1,a+1+n);        a[0] = 0;a[n+1] = INF;

    int lim = 10*MX;
    int mx = 0;

    for (int i=1;i<=n;i++){
        if(a[i] != a[i-1])
        for (int j=a[i]+a[i];j<=lim;j+=a[i]){
            int pos = LB(a+1,a+1+n,j)-a-1;

            if (a[pos] <= a[i]) {}
            else mx = max( a[pos]%a[i],mx );

            if (pos == n)       break;
        }
    }

    cout << mx << endl;

return 0;
}


/// بسم الله الرحمن الرحيم

/* *********************************************************************
   *                       JU_Undefined_Coders                         *
   *               Team Members: Shaheen, Niloy, B'ru                  *
   ********************************************************************* */

#include<algorithm>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#include<deque>
#include<climits>
#include<complex>

#define S scanf
#define P printf

#define I1(n) scanf("%d",&n)
#define LL1(n) scanf("%lld",&n)
#define I2(n1,n2) scanf("%d%d",&n1,&n2)
#define LL2(n1,n2) scanf("%lld%lld",&n1,&n2)
#define I3(n1,n2,n3) scanf("%d%d%d",&n1,&n2,&n3)
#define LL3(n1,n2,n3) scanf("%lld%lld%lld",&n1,&n2,&n3)

#define  F(i,a,b) for(i=a;i<b; i++)
#define FF(i,a,b) for(i=a;i<=b;i++)
#define FI(i,a,b) for((typeof(b)) i=a;i<=b;i++)
#define FR(i, a, b,inc) for(  i = (a); i <= (b); i+=inc )
#define RFR(i, a, b,dec) for(  i = (a); i >= (b); i-=dec )
#define FS(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define PR(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;

#define LL long long int
#define ULL unsigned long long int
#define D double

#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define _Max(a,b,c) Max(a,Max(b,c))
#define _Min(a,b,c) Min(a,Min(b,c))
#define SQR(n) (n*n)
#define PI 2.0*acos(0.0) //#define PI acos(-1.0)

#define all(a) a.begin(),a.end()
#define X first
#define Y second

#define PCC cout<<"Case "<<cas++<<": ";
#define PC printf("Case %d:",cas++);
#define PCN printf("Case %d:\n",cas++);
#define NL printf("\n");
#define cp printf("here\n");

#define SET(a,i) memset(a,i,sizeof a)
#define MEM(a,val) memset(a,val,sizeof(a))
#define open freopen("input.txt","r",stdin)
#define close freopen ("output.txt","w",stdout)
#define Case(a) printf("Case %d:",a)
#define caseh(a) printf("Case #%d: ",a)
#define S1(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL1(a) scanf("%lld",&a)
#define SL2(a,b) scanf("%lld%lld",&a,&b)
#define SL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define CHR getchar()
#define PB(x) push_back(x)
#define pb push_back
#define PP pop_back()
#define PF(x) push_front(x)
#define PPF(x) pop_front()
#define PS(x) push(x)
#define sz() size()
#define UB upper_bound
#define LB lower_bound
#define mxe(a,n) (*max_element(a,a+n))
#define mne(a,n) (*min_element(a,a+n))
#define SUM(a,n) (accumulate(a,a+n,0))
#define onbits  __builtin_popcount
#define VI vector<int>
#define DBG(n) P("Done %d\n",n);

using namespace std;

template <typename T> T BigMod (T b,T p,T m){if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;}
template <typename T> T ModInv (T b,T m){return BigMod(b,m-2,m);}
template <typename T> void ia (T a[],int n){for (int i=0; i<n; i++) cin >> a[i];}
template <typename T> void pa (T a[],int n){for (int i=0; i<n-1; i++) cout << a[i] << " ";cout << a[n-1] << endl;}
template <typename T> LL isLeft(T a,T b,T c) { return (a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y); }
template <typename T> T in(){char ch;T n = 0;bool ng = false;while (1){ch = getchar();if (ch == '-'){ng = true;ch = getchar();break;}if (ch>='0' && ch<='9')     break;}while (1){n = n*10 + (ch - '0');ch = getchar();if (ch<'0' || ch>'9')   break;}return (ng?-n:n);}
template<typename T>inline T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
template<typename T>inline T Bigmod(T b,T p,T m){ if(p==0) return 1; else if (!(p&1)) return SQR(Bigmod(b,p/2,m)) % m;else return ((b % m) * Bigmod(b,p-1,m)) % m;}
template<typename T>inline T Dis(T x1,T y1,T x2, T y2){return sqrt( SQR(x1-x2) + SQR(y1-y2) );}
template<typename T>inline T Angle(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
template<typename T>inline T DIFF(T a,T b) { T d = a-b;if(d<0)return -d;else return d;}
template<typename T>inline T ABS(T a) {if(a<0)return -a;else return a;}
template<typename T>inline T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<typename T>inline T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<typename T>inline T euclide(T a,T b,T &x,T &y) {if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}   if(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}   if(b==0){x=1;y=0;return a;}else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}}

int Set(int N,int pos){ return N=N | (1<<pos);}
int Reset(int N,int pos){return N= N & ~(1<<pos);}
bool Check(int N,int pos){return (bool)(N & (1<<pos));}
int toInt(string s)  { int sm; stringstream ss(s); ss>>sm; return sm; }
int toLlint(string s) { long long int sm; stringstream ss(s); ss>>sm; return sm;}
int cdigittoint(char ch){return ch-'0';}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
bool isConst(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}

/* ------------------------------------------------------------------------------------------------------- */
//          0123456789
#define MX  200007
#define MOD 1000000007
#define INF (1<<30)-1+(1<<30)
#define eps 1e-9

int a[MX];
int prev[MX];

int main()
{
    int n;

    S1(n);

    for (int i=1;i<=n;i++)      S1(a[i]);

    sort(a+1,a+1+n);        a[0] = 0;a[n+1] = INF;

    int lim = 10*MX;
    int mx = 0;

    for (int i=1;i<=n;i++){
        for (int j=i+i;j<=lim;j+=i){
            int pos = LB(a+1,a+1+n,j)-a-1;

            //if (a[pos] < a[i])     continue;

            mx = max( a[pos]%a[i],mx );

            if (pos == n)       break;
        }
    }

    cout << mx << endl;

return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define speed                     \
	ios_base::sync_with_stdio(0); \
	cin.tie(0), cout.tie(0);
using namespace std;
int w = 400000, a[400000];
int main()
{
	fastio;
	int n, l, r, q;
	cin >> n;
	l = 199999;
	r = 200000;
	while (n--)
	{
		char b;
		int m;
		cin >> b >> m;
		if (b == 'L')
			a[l--] = m;
		else if (b == 'R')
			a[r++] = m;
		else
		{
			int k;
			k = find(a + l, a + r, m) - a;
			cout << min(k - l - 1, r - k - 1) << "\n";
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
int a[400000];
int main()
{
	int n, l = 199999, r = 200000;
	scanf("%d", &n);
	while (n--)
	{
		string b;
		int m;
		scanf("%s%d", &b, &m);
		if (b == "L")
			a[l--] = m;
		else if (b == "R")
			a[r++] = m;
		else
		{
			int k;
			k = find(a + l, a + r, m) - a;
			k = min(k - l - 1, r - k - 1);
			printf("%d\n", k);
		}
	}
}


#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <map>
using namespace std;
map < int , int > r;
map < int , int > l;
map < int , char > m;
vector < int > ans;
int cnt_l,cnt_r;
int search_L(int x){
    //cout<<l[x]<<endl;
    return cnt_l-(l[x]+1);
}
int search_R(int x){
    //cout<<r[x]<<endl;
        return cnt_r-(r[x]+1);
}
int solve(int x){
    int L,R,ind;
    bool chk=true;
  //cout<<cnt_l<<' '<<cnt_r<<endl;
    if(m[x]=='R')
        ind=search_R(x);
    if(m[x]=='L'){
        ind=search_L(x);
        chk=false;
        }
    //cout<<ind<<endl;
    if(chk){
        L=cnt_l+(cnt_r-ind);
        R=ind;
    //cout<<L<<' '<<R<<endl;
    }
    else
    {
        L=ind;
        R=cnt_r+l[x];
        //cout<<L<<' '<<R<<endl;
    }
    return min(L,R);
}
int main()
{  int q,x;
   cnt_l=0,cnt_r=0;
   char c;
   cin>>q;
   while(q--){
      cin>>c>>x;
      if(c=='L')
        {   l[x]=cnt_l;
            cnt_l++;
        }
      if(c=='R')
        {   r[x]=cnt_r;
            cnt_r++;
        }
      if(c=='?'){
        ans.push_back(solve(x));
        continue;
        }
    m[x]=c;
   }
  for(int i=0;i<ans.size();i++)
  cout<<ans[i]<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <map>
using namespace std;
vector < int > r;
vector < int > l;
map < int , char > m;
vector < int > ans;
int search_L(int x){
    for(int i=0;i<l.size();i++)
        if(l[i]==x)return i;
}
int search_R(int x){
        for(int i=0;i<r.size();i++)
        if(r[i]==x)return i;
}
int solve(int x){
    int L,R,ind;
    bool chk=true;
    if(m[x]=='R')
        ind=search_R(x);
    if(m[x]=='L'){
        ind=search_L(x);
        chk=false;
        }
    if(chk){
        L=l.size()+(r.size()-ind-1);
        R=ind;
    }
    else
    {
        L=ind;
        R=r.size()+(l.size()-ind-1);
    }
    return min(L,R);
}
int main()
{  int q,x;
   char c;
   cin>>q;
   while(q--){
      cin>>c>>x;
      if(c=='L')
        l.insert(l.begin(),x);
      if(c=='R')
        r.insert(r.begin(),x);
      if(c=='?'){
        ans.push_back(solve(x));
        continue;
        }
    m[x]=c;
   }
  for(int i=0;i<ans.size();i++)
  cout<<ans[i]<<endl;
    return 0;
}