#include <iostream>
#include <ctype.h>
using namespace std;
int main(){
    string layout1, layout2, word;
    cin >> layout1 >> layout2 >> word;

    bool uppers[word.size()];
    for(int lol = 0 ; lol < word.size() ; lol++){
        uppers[lol] = 0;
    }
    for(int lol = 0 ; lol < word.size() ; lol++){
        if( isupper(word.at(lol)) ){
            uppers[lol] = 1;
            word.at(lol) = tolower(word.at(lol));
        }
    }
    for(int lol = 0 ; lol < word.size() ; lol++){
        int index;
        if (isalpha( word.at(lol) ))
            index = layout1.find(word.at(lol));
        else
            continue;
        word.at(lol) = layout2.at(index);
    }
    for(int lol = 0 ; lol < word.size() ; lol++){
        if(uppers[lol])
            word.at(lol) = toupper(word.at(lol));
    }
    cout << endl << word << endl;

    return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main() {
	int a = 0;
	char ANS[1000];
	string s1, s2, s3;
	cin >> s1 >> s2 >> s3;
	for (int i = 0; i < s3.length(); ++i)
		if (s3[i] >= 48 && s3[i] <= 57)
			ANS[a++] = s3[i];
		else
			for (int j = 0; j < 26; ++j)
				if (s3[i] == s1[j] || s3[i] == s1[j] - 32) {
					if (s3[i] <= 90)
						ANS[a++] = s2[j] - 32;
					else
						ANS[a++] = s2[j];
					break;
				}
	for (int i = 0; i < a; ++i)
		cout << ANS[i];
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
    string s,sa,sb,so;
    int i,j,in;
    cin >> sa >> sb >> s;
    for(i=0;i<s.length();i++)
    {
        if((s[i]>=97&&s[i]<=122)||(s[i]>=65&&s[i]<=90))
        {
            for(j=0;j<26;j++)
            {
                char c;
                c=s[i];
                if(tolower(c)==sa[j]) {
                    in=j;
                    break;
                }
            }
            if(isupper(s[i]))
                so.push_back(toupper(sb[in]));
            else
                so=so+sb[in];
        }
        else so=so+s[i];
    }
    cout << so;
    return 0;
}


var k1 = readline();
var k2 = readline();
var inp = readline();

var i = 0, j = 0, z = 0, l = inp.length, out = "";

for(i = 0; i< l; i++)
{
  // ������� �����
  if(inp.charCodeAt(i) >= 65 && inp.charCodeAt(i) <= 90)
  {
    z = inp.charCodeAt(i)+32;
    for(j = 0; j< 26; j++)
    {
      if(k1.charCodeAt(j) == z)
      {
        out = out + k2[j].toUpperCase();
      }
    }
  }
  else if(inp.charCodeAt(i) >= 97 && inp.charCodeAt(i) <= 122)
  // ��������� �����
  {
    for(j = 0; j< 26; j++)
    {
      z = inp.charCodeAt(i);
      if(k1.charCodeAt(j) == z)
      {
        out = out + k2[j];
      }
    }

  }
  // ��� ���������
  else
  {
    out = out + inp[i];
  }
}

print(out);


var from = readline().split('')
var to = readline().split('')
var convert = readline().split('')
print(convert.map(x => !isNaN(x) ? x : to[from.indexOf(x)] || to[from.indexOf(x.toLowerCase())].toUpperCase() ).join(''))


var layout1 = readline();
var layout2 = readline();
var input = readline();

var output = '';
for(var i=0; i<input.length; i++){
	if(isNaN(input[i])){
		var isLower = (input[i]+'').toLowerCase()==(input[i]+'');
		var char = isLower?(input[i]+''):(input[i]+'').toLowerCase();
		output+=isLower?layout2[layout1.indexOf(char)]:layout2[layout1.indexOf(char)].toUpperCase();
	}else{
		output+=input[i];
	}
}
write(output);


var layout1 = readLine();
var layout2 = readLine();
var input = readLine();

var output = '';
for(var i=0; i<input.length; i++){
	if(isNaN(input[i])){
		var isLower = (input[i]+'').toLowerCase()==(input[i]+'');
		var char = isLower?(input[i]+''):(input[i]+'').toLowerCase();
		output+=isLower?layout2[layout1.indexOf(char)]:layout2[layout1.indexOf(char)].toUpperCase();
	}else{
		output+=input[i];
	}
}
print(output);


var firstLayout = readline()
var secondLayout = readline()

var input = readline();
var output = '';

for (var i = 0; i < input.length; i++) {
  var char = input[i];

  if (~'0123456789'.indexOf(char)) {
    output += char;
    continue;
  }

  if (char === char.toUpperCase()) {
    var LayoutCharPos = firstLayout.indexOf(char.toLowerCase());
    output += secondLayout[LayoutCharPos].toUpperCase();
  } else {
    var LayoutCharPos = firstLayout.indexOf(char);
    output += secondLayout[LayoutCharPos];
  }
}

print(output);


function derecording(recorder, derecorder, record){
    var result = '';

    for (var i = 0; i < record.length; i++){
        var number = recorder.indexOf(record[i]);
        var numberUpper = recorder.indexOf(record[i].toLowerCase());
        if (number >= 0 ){
            result += derecorder[number];
        } else if(numberUpper >= 0){
            result += derecorder[numberUpper].toUpperCase();
        } else if (!isNaN(parseInt(record[i]))){
            result += record[i];
        }
    }

    return result;
}
print(derecording(readline(),readline(), readline());


    var recorder = readline(),
        derecorder = readline(),
        record = readline(),
        result = '';

    for (var i = 0; i < record.length; i++){
        var number = recorder.indexOf(record[i]);
        var numberUpper = recorder.indexOf(record[i].toLowerCase());
        if (number >= 0 ){
            result += derecorder[number];
        } else if(numberUpper >= 0){
            result += derecorder[numberUpper].toUpperCase();
        } else if (!isNaN(parseInt(record[i]))){
            result += record[i];
        }
    }

print(result);


function derecording(record, recorder, derecorder){
    var result = '';

    for (var i = 0; i < record.length; i++){
        var number = recorder.indexOf(record[i]);
        var numberUpper = recorder.indexOf(record[i].toLowerCase());
        if (number >= 0 ){
            result += derecorder[number];
        } else if(numberUpper >= 0){
            result += derecorder[numberUpper].toUpperCase();
        } else if (!isNaN(parseInt(record[i]))){
            result += record[i];
        }
    }

    return result;
}


alph1 = readline();
alph2 = readline();
s = readline();
s1 = '';
for(var i=0; i< s.length; i++) {
    var char = s[i];
    if (/[0-9]/.test(char)) {
        s1 = s1 + char;
        continue;
    }
    var lowChar = s[i].toLowerCase();
    var resChar = alph2[alph1.indexOf(lowChar)];
    if (char != lowChar) {
        resChar = resChar.toUpperCase();
    }
    s1 = s1 + resChar;
}

print(s1);


var fr = readline().split('')
var to = readline().split('')
var convert = readline().split('')
write(convert.map(x => !isNaN(x) ? x : to[fr.indexOf(x)] || to[fr.indexOf(x.toLowerCase())].toUpperCase()).join(''))


(function main(){
  //http://codeforces.com/problemset/problem/831/B
  var layout1Minusculas = readline();
  var layout1Mayusculas = layout1Minusculas.toUpperCase();
  var layout2Minusculas = readline();
  var layout2Mayusculas = layout2Minusculas.toUpperCase();
  var datos = readline();

  var salida = "";
  for (var i = 0; i < datos.length; i++) {
    if(!isNaN((datos[i]))){
      salida += datos[i];
    }else if(caracterEsMayuscula(i)){
      traducir(i, layout1Mayusculas, layout2Mayusculas);
    }else{
      traducir(i, layout1Minusculas, layout2Minusculas);
    }
  }

  function caracterEsMayuscula(index){
    var charcode = datos.charCodeAt(index);
    return  charcode >= 65 && charcode <= 90;
  }

  function traducir(index, layoutInicial, layoutFinal){
    var indexLayout = layoutInicial.indexOf(datos[i]);
    salida += layoutFinal.charAt(indexLayout);
  }

  print(salida);
})();


#include <iostream>
using namespace std;
# define FOR(i,a,n) for(int i=a;i<n;i++)
# define rep(i,n) FOR(i,0,n)
# define pb push_back
typedef long long int ll;
const int N=2000000+500;
const int MAXN=1e6+1;
long long ans;

int main()
{
    ios_base::sync_with_stdio(false);
    string a,b,c,a2,b2;
    cin>>a>>b>>c;
    int n=a.size();
    a2=a;
    b2=b;
    rep(i,n)
    {
        a2[i]=a2[i]-'a'+'A';
        b2[i]=b2[i]-'a'+'A';
    }
    n=c.size();
    bool f=0;
    rep(i,n)
    {
        f=0;
        if(c[i]>='A'){
            if(c[i]<='Z')
            {
                int j=0;f=0;
                while(!f)
                {
                    if(c[i]==a2[j])
                    {
                        cout<<b2[j];
                        f=1;
                    }
                    j++;
                }
            }
        }
        if(c[i]>='a'){
            if(c[i]<='z')
            {
                int j=0;f=0;
                while(!f)
                {
                    if(c[i]==a[j])
                    {
                        cout<<b[j];
                        f=1;
                    }
                    j++;
                }
            }
        }
        if(!f)
            cout<<c[i];
    }
    return 0;
}


#include <iostream>
using namespace std;
# define FOR(i,a,n) for(int i=a;i<n;i++)
# define rep(i,n) FOR(i,0,n)
# define pb push_back
typedef long long int ll;
const int N=2000000+500;
const int MAXN=1e6+1;
long long ans;

int main()
{
    ios_base::sync_with_stdio(false);
    string a,b,c,a2,b2;
    cin>>a>>b>>c;
    int n=a.size();
    a2=a;
    b2=b;
    rep(i,n)
    {
        a2[i]=a2[i]-'a'+'A';
        b2[i]=b2[i]-'a'+'A';
    }
    n=c.size();
    bool f=0;
    rep(i,n)
    {
        f=0;
        if(c[i]>='A'){
            if(c[i]<='Z')
            {
                int j=0;f=0;
                while(!f)
                {
                    if(c[i]==a[j])
                    {
                        cout<<b[j];
                        f=1;
                    }
                }
            }
        }
        if(c[i]>='a'){
            if(c[i]<='z')
            {
                int j=0;f=0;
                while(!f)
                {
                    if(c[i]==a[j])
                    {
                        cout<<b[j];
                        f=1;
                    }
                }
            }
        }
        if(!f)
            cout<<c[i];
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 1000007
#define N 8
#define M 12357
#define ll long long
using namespace std;
map<int,int>mp;
int main()
{
	printf("%d %d\n",'a','A');
	char s1[30],s2[30],s3[30];
	scanf("%s%s%s",s1,s2,s3);
	for(int i=0;i<strlen(s1);i++)
	{
		mp[s1[i]]=s2[i];

	}
	for(int i=0;i<strlen(s3);i++)
	{
		if(s3[i]>='a'&&s3[i]<='z')
		{
			printf("%c",tolower(mp[s3[i]]));

		}
		else if(s3[i]>='A'&&s3[i]<='Z')
		{

			printf("%c",toupper(mp[s3[i]+('a'-'A')]));

		}
		else
		{
			printf("%c",s3[i]);

		}

	}
	puts("");

	return 0;
}
//qwertyuiopasdfghjklzxcvbnm
//veamhjsgqocnrbfxdtwkylupzi
//TwccpQZAvb2017


#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 1000007
#define N 8
#define M 12357
#define ll long long
using namespace std;
map<int,int>mp;
int main()
{
	//printf("%d %d\n",'a','A');
	char s1[30],s2[30],s3[1010];
	scanf("%s%s%s",s1,s2,s3);
	for(int i=0;i<strlen(s1);i++)
	{
		//if(s3[i]>='a'&&s3[i]<='z')
		mp[s1[i]]=s2[i];
	}
	for(int i=0;i<strlen(s3);i++)
	{
		if(s3[i]>='a'&&s3[i]<='z')
		{
			printf("%c",tolower(mp[s3[i]]));
		}
		else if(s3[i]>='A'&&s3[i]<='Z')
		{
			printf("%c",toupper(mp[s3[i]+('a'-'A')]));
		}
		else
		{
			printf("%c",s3[i]);
		}

	}
	puts("");
	return 0;
}
//qwertyuiopasdfghjklzxcvbnm
//veamhjsgqocnrbfxdtwkylupzi
//TwccpQZAvb2017


#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 1000007
#define N 8
#define M 12357
#define ll long long
using namespace std;
map<int,int>mp;
int main()
{
	//printf("%d %d\n",'a','A');
	char s1[30],s2[30],s3[30];
	scanf("%s%s%s",s1,s2,s3);
	for(int i=0;i<strlen(s1);i++)
	{
		//if(s3[i]>='a'&&s3[i]<='z')
		mp[s1[i]]=s2[i];

	}
	for(int i=0;i<strlen(s3);i++)
	{
		if(s3[i]>='a'&&s3[i]<='z')
		{
			printf("%c",tolower(mp[s3[i]]));

		}
		else if(s3[i]>='A'&&s3[i]<='Z')
		{

			printf("%c",toupper(mp[s3[i]+('a'-'A')]));

		}
		else
		{
			printf("%c",s3[i]);

		}

	}
	puts("");

	return 0;
}
//qwertyuiopasdfghjklzxcvbnm
//veamhjsgqocnrbfxdtwkylupzi
//TwccpQZAvb2017


#include <cstring>
#include <algorithm>
#include <cctype>
#include <cstdio>

using namespace std;
char A[27];
char B[27];
int MAP[27];
char C[1001];

int main() {
    scanf("%s %s %s", A, B, C);
    for (int i = 0; i < 26; i++)
        MAP[A[i] - 'a'] = A[i] - B[i];
    int len = strlen(C);
    for (int i = 0; i < len; i++) {
        char c = C[i];
        if (isalpha(c))
            printf("%c", c - MAP[tolower(c) - 'a']);
        else
            printf("%c", c);
    }
    printf("\n");
    return 0;
}


#include <cstring>
#include <algorithm>
#include <cctype>

using namespace std;
char A[27];
char B[27];
int MAP[27];
char C[1001];

int main() {
    scanf("%s %s %s", A, B, C);
    for (int i = 0; i < 26; i++)
        MAP[A[i] - 'a'] = A[i] - B[i];
    int len = strlen(C);
    for (int i = 0; i < len; i++) {
        char c = C[i];
        if (isalpha(c))
            printf("%c", c - MAP[tolower(c) - 'a']);
        else
            printf("%c", c);
    }
    printf("\n");
    return 0;
}


#include <iostream>
using namespace std;
int main(){
    string s1,s2,s;
    cin>>s1>>s2>>s;
    char c[26];
    for(int i=0;i<26;i++){
        c[s1[i]-'a']=s2[i];
    }for(int i=0;i<s.length();i++)
        cout<<c[s[i]-'a'];
}


#include <bits/stdc++.h>
#define num long long
#define rip(i,f,n) for(int i=f;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define put(x) cout<<#x<<" = "<<x<<endl
const int maxn=1e5+10, mod=1e9+7, inf=2e9;
using namespace std;

num n=5,k,t[26];
string a,b,c;
int main()
{
    ios::sync_with_stdio(false),cin.tie(),cout.tie();
    cin>>a>>b>>c;
    rep(i,a.size()) t[a[i]]=b[i];
    rep(i,c.size())
    {
        if(c[i]>='0' && c[i]<='9') cout<<c[i];
        if(c[i]>='A' && c[i]<='Z')
        {
            cout<<char(t[c[i]-'A'+'a']+'A'-'a');
        }else
        {
            cout<<char(t[c[i]]);
        }
    }
}


#include <bits/stdc++.h>
#define num long long
#define rip(i,f,n) for(int i=f;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define put(x) cout<<#x<<" = "<<x<<endl
const int maxn=1e5+10, mod=1e9+7, inf=2e9;
using namespace std;

num n=5,k,t[1000];
string a,b,c;
int main()
{
    ios::sync_with_stdio(false),cin.tie(),cout.tie();
    cin>>a>>b>>c;
    rep(i,a.size()) t[a[i]]=b[i];
    rep(i,c.size())
    {
        if(c[i]>='0' && c[i]<='9') cout<<c[i];
        if(c[i]>='A' && c[i]<='Z')
        {
            cout<<char(t[c[i]-'A'+'a']+'A'-'a');
        }else
        {
            cout<<char(t[c[i]]);
        }
    }
}


#include <bits/stdc++.h>
#define num long long
#define rip(i,f,n) for(int i=f;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define put(x) cout<<#x<<" = "<<x<<endl
const int maxn=1e5+10, mod=1e9+7, inf=2e9;
using namespace std;

num n=5,k,t[1000];
string a,b,c;
int main()
{
    ios::sync_with_stdio(false),cin.tie(),cout.tie();
    cin>>a>>b>>c;
    rep(i,a.size()) t[a[i]]=b[i];
    rep(i,c.size())
    {
        if(c[i]>='0' && c[i]<='9') cout<<c[i];
        if(c[i]>='A' && c[i]<='Z')
        {
            cout<<char(t[c[i]-'A'+'a']+'A'-'a');
        }else
        {
            cout<<char(t[c[i]]);
        }
    }
    cout<<endl;
}


#include <bits/stdc++.h>
#define num long long
#define rip(i,f,n) for(int i=f;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define put(x) cout<<#x<<" = "<<x<<endl
const int maxn=1e5+10, mod=1e9+7, inf=2e9;
using namespace std;

num n=5,k,t[1000];
string a,b,c;
int main()
{
    ios::sync_with_stdio(false),cin.tie(),cout.tie();
    cin>>a>>b>>c;
    rep(i,a.size()) t[a[i]]=b[i];
    rep(i,c.size())
    {
        if(c[i]>='0' && c[i]<='9') cout<<c[i];
        else if(c[i]>='A' && c[i]<='Z')
        {
            cout<<char(t[c[i]-'A'+'a']+'A'-'a');
        }else
        {
            cout<<char(t[c[i]]);
        }
    }
    cout<<endl;
}


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

/**
 * @author Don Li
 */
public class MaximumValue {

    static final int MAXN = (int) (2e6 + 5);

    void solve() {
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();

        int[] max = new int[MAXN];
        for (int i = 0; i < n; i++) max[a[i]] = a[i];
        for (int i = 1; i < MAXN; i++) if (max[i] == 0) max[i] = max[i - 1];

        int ans = 0;
        for (int i = 1; i < MAXN; i++) {
            if (max[i] == i) {
                for (int j = 2 * i - 1; j < MAXN; j += i) ans = Math.max(ans, max[j] % i);
            }
        }
        out.println(ans);
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new MaximumValue().solve();
        out.close();
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


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

/**
 * @author Don Li
 */
public class MaximumValue {

    static final int MAXN = (int) (2e6 + 5);

    void solve() {
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();

        int[] max = new int[MAXN];
        for (int i = 0; i < n; i++) {
            max[a[i]] = a[i];
        }
        for (int i = 1; i < MAXN; i++) {
            if (max[i] == 0) max[i] = max[i - 1];
        }

        int ans = 0;
        for (int i = 1; i < MAXN; i++) {
            if (max[i] == i) {
                for (int x = 2 * i - 1; x < MAXN; x += i) {
                    ans = Math.max(ans, max[x] % i);
                }
            }
        }
        out.println(ans);
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new MaximumValue().solve();
        out.close();
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


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

/**
 * @author Don Li
 */
public class MaximumValue {

    static final int MAXN = (int) (1e6 + 5);

    void solve() {
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();

        int[] max = new int[MAXN];
        for (int i = 0; i < n; i++) max[a[i]] = a[i];
        for (int i = 1; i < MAXN; i++) if (max[i] == 0) max[i] = max[i - 1];

        int ans = 0;
        for (int i = 1; i < MAXN; i++) {
            if (max[i] == i) {
                for (int j = 2 * i - 1; j < MAXN; j += i) ans = Math.max(ans, max[j] % i);
            }
        }
        out.println(ans);
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new MaximumValue().solve();
        out.close();
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


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

/**
 * @author Don Li
 */
public class MaximumValue {

    static final int MAXN = (int) (1e6 + 5);

    void solve() {
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();

        Arrays.sort(a);

        int res = 0;
        SegmentTreeRMQ rmq = new SegmentTreeRMQ(a);
        for (int i = n - 1; i >= 0; i--) {
            int l = a[i];
            while (l < MAXN) {
                int b = rmq.query(l, l + a[i]);
                res = Math.max(res, b % a[i]);
                l += a[i];
            }
        }
        out.println(res);
    }

    public class SegmentTreeRMQ {

        int n;
        int[] d;

        public SegmentTreeRMQ(int[] A) {
            n = MAXN;
            int m = 0;
            while (1 << m < n) m++;
            n = 1 << m;

            d = new int[2 * n - 1];

            for (int i = 0; i < A.length; i++) {
                d[n - 1 + A[i]] = A[i];
            }
            for (int i = n - 2; i >= 0; i--) {
                d[i] = Math.max(d[2 * i + 1], d[2 * i + 2]);
            }
        }

        public int query(int a, int b) {
            return query(a, b, 0, 0, n);
        }

        public int query(int a, int b, int k, int l, int r) {
            if (a >= r || b <= l) return 0;
            if (a <= l && r <= b) return d[k];
            return Math.max(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r));
        }

        public void update(int i, int v) {
            i += n - 1;
            d[i] = v;
            while (i > 0) {
                i = (i - 1) / 2;
                d[i] = Math.max(d[2 * i + 1], d[2 * i + 2]);
            }
        }
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new MaximumValue().solve();
        out.close();
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

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

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

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

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

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


import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int no = Integer.parseInt(in.readLine() );
        StringTokenizer st = new StringTokenizer(in.readLine() );
        int[] l1 = new int[no];
        for (int i = 0; i < l1.length; i++){
            l1[i] = Integer.parseInt(st.nextToken() );
        }
        {
            Arrays.sort(l1);
            int size = 0;
            for (int i = 0 ; i < l1.length; i++){
                if (i == 0 || l1[i] != l1[i - 1]){
                    l1[size++] = l1[i];
                }
            }
            l1 = Arrays.copyOf(l1, size);
        }
        int[] lastMax = new int[1000000 + 100];
        Arrays.fill(lastMax, -1);
        int ptr = 0;
        for (int i = l1[0] + 1; i < lastMax.length; i++){
            for (;ptr < l1.length - 1;){
                if (l1[ptr + 1] < i){
                    ptr++;
                }
                else break;
            }
            lastMax[i] = l1[ptr];
        }
//      px(l1);
        int ans = 0;
        for (int e: l1){
            for (int i = e; ; i += e){
                int maxEle = getMaxEle(lastMax, i);
//              px(e, i, maxEleIdx);
                if (maxEle >= 0){

                    if (maxEle > e){
//                      ans = l1[maxEleIdx] % e;
                        ans = Math.max(ans, maxEle % e);
                    }
                }
                if (i >= l1[l1.length - 1]) break;
            }
//          px("e", e, ans);
        }

        out.println(ans);

        out.flush();
    }
    static int getMaxEle(int[] dpList, int lim){
        if (lim >= dpList.length){
            return dpList[dpList.length - 1];
        }
        return dpList[lim];
    }
    static int maxEle(int[] l1, int lim){
        if (l1[0] >= lim){
            return -1;
        }
        if (l1[l1.length - 1] < lim) return l1.length - 1;
        int low = 0;
        int high = l1.length - 1;
        for (;high - low > 1;){
            int mid = (low + high) >> 1;
            if (l1[mid] < lim) low = mid;
            else high = mid;
        }
        return low;
    }
    static void px(Object ...objects){System.out.println(Arrays.deepToString(objects) ); }
    static PrintWriter out = new PrintWriter(System.out);
}


import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int no = Integer.parseInt(in.readLine() );
        StringTokenizer st = new StringTokenizer(in.readLine() );
        int[] l1 = new int[no];
        for (int i = 0; i < l1.length; i++){
            l1[i] = Integer.parseInt(st.nextToken() );
        }
        {
            Arrays.sort(l1);
            int size = 0;
            for (int i = 0 ; i < l1.length; i++){
                if (i == 0 || l1[i] != l1[i - 1]){
                    l1[size++] = l1[i];
                }
            }
            l1 = Arrays.copyOf(l1, size);
        }
//      px(l1);
        int ans = 0;
        for (int e: l1){
            for (int i = e; ; i += e){
                int maxEleIdx = maxEle(l1, i);
//              px(e, i, maxEleIdx);
                if (maxEleIdx >= 0){

                    if (l1[maxEleIdx] > e){
                        ans = Math.max(ans, l1[maxEleIdx] % e);
                    }
                }
                if (i >= l1[l1.length -1 ]) break;
            }
//          px("e", e, ans);
        }

        out.println(ans);

        out.flush();
    }
    static int maxEle(int[] l1, int lim){
        if (l1[0] >= lim){
            return -1;
        }
        if (l1[l1.length - 1] < lim) return l1.length - 1;
        int low = 0;
        int high = l1.length - 1;
        for (;high - low > 1;){
            int mid = (low + high) >> 1;
            if (l1[mid] < lim) low = mid;
            else high = mid;
        }
        return low;
    }
    static void px(Object ...objects){System.out.println(Arrays.deepToString(objects) ); }
    static PrintWriter out = new PrintWriter(System.out);
}


import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int no = Integer.parseInt(in.readLine() );
        StringTokenizer st = new StringTokenizer(in.readLine() );
        int[] l1 = new int[no];
        for (int i = 0; i < l1.length; i++){
            l1[i] = Integer.parseInt(st.nextToken() );
        }
//      for (int i = 0 ; i < l1.length - 1; i++){
//          int rnd = (int) (Math.random() * (l1.length - i - 1) ) + (i + 1);
//          int temp = l1[i];
//          l1[i] = l1[rnd];
//          l1[rnd] = temp;
//      }
        {
            Arrays.sort(l1);
            int size = 0;
            for (int i = 0 ; i < l1.length; i++){
                if (i == 0 || l1[i] != l1[i - 1]){
                    l1[size++] = l1[i];
                }
            }
            l1 = Arrays.copyOf(l1, size);
        }
//      px(l1);
        int ans = 0;
        for (int e: l1){
            for (int i = e; ; i += e){
                int maxEleIdx = maxEle(l1, i);
//              px(e, i, maxEleIdx);
                if (maxEleIdx >= 0){

                    if (l1[maxEleIdx] > e && l1[maxEleIdx] % e > ans){
                        ans = l1[maxEleIdx] % e;
//                      ans = Math.max(ans, l1[maxEleIdx] % e);
                    }
                }
                if (i >= l1[l1.length -1 ]) break;
            }
//          px("e", e, ans);
        }

        out.println(ans);

        out.flush();
    }
    static int maxEle(int[] l1, int lim){
        if (l1[0] >= lim){
            return -1;
        }
        if (l1[l1.length - 1] < lim) return l1.length - 1;
        int low = 0;
        int high = l1.length - 1;
        for (;high - low > 1;){
            int mid = (low + high) >> 1;
            if (l1[mid] < lim) low = mid;
            else high = mid;
        }
        return low;
    }
    static void px(Object ...objects){System.out.println(Arrays.deepToString(objects) ); }
    static PrintWriter out = new PrintWriter(System.out);
}


import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int no = Integer.parseInt(in.readLine() );
        StringTokenizer st = new StringTokenizer(in.readLine() );
        int[] l1 = new int[no];
        for (int i = 0; i < l1.length; i++){
            l1[i] = Integer.parseInt(st.nextToken() );
        }
        for (int i = 0 ; i < l1.length - 1; i++){
            int rnd = (int) (Math.random() * (l1.length - i - 1) ) + (i + 1);
            int temp = l1[i];
            l1[i] = l1[rnd];
            l1[rnd] = temp;
        }
        {
            Arrays.sort(l1);
            int size = 0;
            for (int i = 0 ; i < l1.length; i++){
                if (i == 0 || l1[i] != l1[i - 1]){
                    l1[size++] = l1[i];
                }
            }
            l1 = Arrays.copyOf(l1, size);
        }
//      px(l1);
        int ans = 0;
        for (int e: l1){
            for (int i = e; ; i += e){
                int maxEleIdx = maxEle(l1, i);
//              px(e, i, maxEleIdx);
                if (maxEleIdx >= 0){

                    if (l1[maxEleIdx] > e){
                        ans = Math.max(ans, l1[maxEleIdx] % e);
                    }
                }
                if (i >= l1[l1.length -1 ]) break;
            }
//          px("e", e, ans);
        }

        out.println(ans);

        out.flush();
    }
    static int maxEle(int[] l1, int lim){
        if (l1[0] >= lim){
            return -1;
        }
        if (l1[l1.length - 1] < lim) return l1.length - 1;
        int low = 0;
        int high = l1.length - 1;
        for (;high - low > 1;){
            int mid = (low + high) >> 1;
            if (l1[mid] < lim) low = mid;
            else high = mid;
        }
        return low;
    }
    static void px(Object ...objects){System.out.println(Arrays.deepToString(objects) ); }
    static PrintWriter out = new PrintWriter(System.out);
}


import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int no = Integer.parseInt(in.readLine() );
        StringTokenizer st = new StringTokenizer(in.readLine() );
        int[] l1 = new int[no];
        for (int i = 0; i < l1.length; i++){
            l1[i] = Integer.parseInt(st.nextToken() );
        }
        for (int i = 0 ; i < l1.length; i++){
            int rnd = (int) (Math.random() * (l1.length - i - 1) ) + (i + 1);
            int temp = l1[i];
            l1[i] = l1[rnd];
            l1[rnd] = temp;
        }
        {
            Arrays.sort(l1);
            int size = 0;
            for (int i = 0 ; i < l1.length; i++){
                if (i == 0 || l1[i] != l1[i - 1]){
                    l1[size++] = l1[i];
                }
            }
            l1 = Arrays.copyOf(l1, size);
        }
//      px(l1);
        int ans = 0;
        for (int e: l1){
            for (int i = e; ; i += e){
                int maxEleIdx = maxEle(l1, i);
//              px(e, i, maxEleIdx);
                if (maxEleIdx >= 0){

                    if (l1[maxEleIdx] > e){
                        ans = Math.max(ans, l1[maxEleIdx] % e);
                    }
                }
                if (i >= l1[l1.length -1 ]) break;
            }
//          px("e", e, ans);
        }

        out.println(ans);

        out.flush();
    }
    static int maxEle(int[] l1, int lim){
        if (l1[0] >= lim){
            return -1;
        }
        if (l1[l1.length - 1] < lim) return l1.length - 1;
        int low = 0;
        int high = l1.length - 1;
        for (;high - low > 1;){
            int mid = (low + high) >> 1;
            if (l1[mid] < lim) low = mid;
            else high = mid;
        }
        return low;
    }
    static void px(Object ...objects){System.out.println(Arrays.deepToString(objects) ); }
    static PrintWriter out = new PrintWriter(System.out);
}


//package feb19;

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

public class D485 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0; i<n; i++)
		{
			arr[i]=scn.nextInt();
		}
		int max=Integer.MIN_VALUE;
		Arrays.sort(arr);
		System.out.println(arr[arr.length-1]%arr[0]);

	}

}


//package feb19;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;

public class D485 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int[] arr=new int[n];
		int[] freq=new int[2000005];
		TreeSet<Integer> set=new TreeSet<>();
		/*TreeSet will arrange in sorted order and also it does not add duplicates :)*/
		for(int i=0; i<n; i++)
		{
			arr[i]=scn.nextInt();
			set.add(arr[i]);
			if(freq[arr[i]]==0)
			{
				freq[arr[i]]=arr[i];
			}
		}
		for(int i=1; i<freq.length; i++)
		{
			if(freq[i]==0)
			{
				freq[i]=freq[i-1];
			}
		}

		int max=Integer.MIN_VALUE;
		//int last=set.last();
		/*Basically the logic is if n=30 then in 30 to 60 than max value of mod is 59%30=29*/
		/*Therefore the in freq array the indexes which are zero are set to the value of previous array
		 * value which ain't zero as we are not sure we will surely land up with 59 when n=30 in
		 * an array with elements 1 2 3 30 58 59 60
		 * etc
		 * */
		for(Integer x: set)
		{
			//System.out.println(x);
			for(int j=2*x; j<=freq.length; j=j+x)
			{
				int index=freq[j-1];
				//System.out.print("Index:"+index);
				if(index>x)
				{
				max=Math.max(max, index%x);
				}
			}
		//	System.out.println();
		//	System.out.println("Max here:"+max);
		//	System.out.println();
		}
		System.out.println(max);

	}

}


//package feb19;

import java.util.Scanner;

public class D485 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0; i<n; i++)
		{
			arr[i]=scn.nextInt();
		}
		int max=Integer.MIN_VALUE;
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				if(arr[i]>=arr[j])
				{
				int x=arr[i]%arr[j];
				if(x>max)
				{
					max=x;
				}
				}
			}
		}
		System.out.println(max);

	}

}


//package feb19;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;

public class D485 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int[] arr=new int[n];
		int[] freq=new int[2000005];
		TreeSet<Integer> set=new TreeSet<>();
		/*TreeSet will arrange in sorted order and also it does not add duplicates :)*/
		for(int i=0; i<n; i++)
		{
			arr[i]=scn.nextInt();
			set.add(arr[i]);
			if(freq[arr[i]]==0)
			{
				freq[arr[i]]=arr[i];
			}
		}
		for(int i=1; i<freq.length; i++)
		{
			if(freq[i]==0)
			{
				freq[i]=freq[i-1];
			}
		}

		int max=Integer.MIN_VALUE;
		//int last=set.last();
		/*Basically the logic is if n=30 then in 30 to 60 than max value of mod is 59%30=29*/
		/*Therefore the in freq array the indexes which are zero are set to the value of previous array
		 * value which ain't zero as we are not sure we will surely land up with 59 when n=30 in
		 * an array with elements 1 2 3 30 58 59 60
		 * etc
		 * */
		for(Integer x: set)
		{
			//System.out.println(x);
			for(int j=2*x; j<=freq.length; j=j+x)
			{
				int index=freq[j-1];
				//System.out.print("Index:"+index);
				if(index>=x)
				{
				max=Math.max(max, index%x);
				}
			}
		//	System.out.println();
		//	System.out.println("Max here:"+max);
		//	System.out.println();
		}
		System.out.println(max);

	}

}


//package feb19;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;

public class D485 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int[] arr=new int[n];
		int[] freq=new int[2000005];
		TreeSet<Integer> set=new TreeSet<>();
		/*TreeSet will arrange in sorted order and also it does not add duplicates :)*/
		for(int i=0; i<n; i++)
		{
			arr[i]=scn.nextInt();
			set.add(arr[i]);
			if(freq[arr[i]]==0)
			{
				freq[arr[i]]=arr[i];
			}
		}
		for(int i=1; i<freq.length; i++)
		{
			if(freq[i]==0)
			{
				freq[i]=freq[i-1];
			}
		}

		int max=Integer.MIN_VALUE;
		int last=set.last();
		/*Basically the logic is if n=30 then in 30 to 60 than max value of mod is 59%30=29*/
		/*Therefore the in freq array the indexes which are zero are set to the value of previous array
		 * value which ain't zero as we are not sure we will surely land up with 59 when n=30 in
		 * an array with elements 1 2 3 30 58 59 60
		 * etc
		 * */
		for(Integer x: set)
		{
		//	System.out.println(x);
			for(int j=2*x; j<=last && j<=freq.length; j=j+x)
			{
				int index=freq[j-1];
			//	System.out.print("Index:"+index);
				if(index>x)
				{
				max=Math.max(max, index%x);
				}
			}
		//	System.out.println();
		//	System.out.println("Max here:"+max);
		//	System.out.println();
		}
		System.out.println(max);

	}

}


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

import static java.lang.Math.*;

public class Main extends PrintWriter {
    BufferedReader in;
    StringTokenizer stok;
    final Random rand = new Random(31);
    final int inf = (int) 1e9;
    final long linf = (long) 1e18;

    public void solve() throws IOException {
        int n = nextInt();
        int[] a = nextIntArray(n);
        shuffle(a);
        Arrays.sort(a);
        int m = (int) 1e6;
        int[] b = new int[m];
        for (int i = 1; i < n; i++) {
            for (int j = a[i - 1]; j < a[i]; j++) {
                b[j] = a[i - 1];
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (i > 0 && a[i] == a[i - 1]) {
                continue;
            }
            ans = max(ans, a[n - 1] % a[i]);
            for (int j = 2 * a[i]; j < m; j += a[i]) {
                ans = max(ans, b[j - 1] % a[i]);
            }
        }
        println(ans);
    }

    public void run() {
        try {
            solve();
            close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(abs(-1));
        }
    }

    Main() throws IOException {
        super(System.out);
        in = new BufferedReader(new InputStreamReader(System.in));
    }

    Main(String s) throws IOException {
        super("".equals(s) ? "output.txt" : (s + ".out"));
        in = new BufferedReader(new FileReader("".equals(s) ? "input.txt" : (s + ".in")));
    }

    public static void main(String[] args) throws IOException {
        try {
            Locale.setDefault(Locale.US);
        } catch (Exception ignored) {
        }
        new Main().run();
    }

    String next() throws IOException {
        while (stok == null || !stok.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null) {
                return null;
            }
            stok = new StringTokenizer(s);
        }
        return stok.nextToken();
    }

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

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

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

    int[] nextIntArray(int len) throws IOException {
        int[] a = new int[len];
        for (int i = 0; i < len; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    void shuffle(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int x = rand.nextInt(i + 1);
            int t = a[i];
            a[i] = a[x];
            a[x] = t;
        }
    }

    boolean nextPerm(int[] p) {
        for (int a = p.length - 2; a >= 0; --a)
            if (p[a] < p[a + 1])
                for (int b = p.length - 1; ; --b)
                    if (p[b] > p[a]) {
                        int t = p[a];
                        p[a] = p[b];
                        p[b] = t;
                        for (++a, b = p.length - 1; a < b; ++a, --b) {
                            t = p[a];
                            p[a] = p[b];
                            p[b] = t;
                        }
                        return true;
                    }
        return false;
    }

    <T> List<T>[] createAdjacencyList(int countVertex) {
        List<T>[] res = new List[countVertex];
        for (int i = 0; i < countVertex; i++) {
            res[i] = new ArrayList<T>();
        }
        return res;
    }
}


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

import static java.lang.Math.*;

public class Main extends PrintWriter {
    BufferedReader in;
    StringTokenizer stok;
    final Random rand = new Random(31);
    final int inf = (int) 1e9;
    final long linf = (long) 1e18;

    public void solve() throws IOException {
        int n = nextInt();
        int[] a = nextIntArray(n);
        shuffle(a);
        Arrays.sort(a);
        int m = (int) 1e6;
        int[] b = new int[m];
        for (int i = 1; i < n; i++) {
            for (int j = a[i - 1]; j < a[i]; j++) {
                b[j] = a[i - 1];
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (i > 0 && a[i] == a[i - 1]) {
                continue;
            }
            ans = max(ans, a[n - 1] % a[i]);
            for (int j = 2 * a[i]; j < m; j += a[i]) {
                ans = max(ans, b[j] % a[i]);
            }
        }
        println(ans);
    }

    public void run() {
        try {
            solve();
            close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(abs(-1));
        }
    }

    Main() throws IOException {
        super(System.out);
        in = new BufferedReader(new InputStreamReader(System.in));
    }

    Main(String s) throws IOException {
        super("".equals(s) ? "output.txt" : (s + ".out"));
        in = new BufferedReader(new FileReader("".equals(s) ? "input.txt" : (s + ".in")));
    }

    public static void main(String[] args) throws IOException {
        try {
            Locale.setDefault(Locale.US);
        } catch (Exception ignored) {
        }
        new Main().run();
    }

    String next() throws IOException {
        while (stok == null || !stok.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null) {
                return null;
            }
            stok = new StringTokenizer(s);
        }
        return stok.nextToken();
    }

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

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

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

    int[] nextIntArray(int len) throws IOException {
        int[] a = new int[len];
        for (int i = 0; i < len; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    void shuffle(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int x = rand.nextInt(i + 1);
            int t = a[i];
            a[i] = a[x];
            a[x] = t;
        }
    }

    boolean nextPerm(int[] p) {
        for (int a = p.length - 2; a >= 0; --a)
            if (p[a] < p[a + 1])
                for (int b = p.length - 1; ; --b)
                    if (p[b] > p[a]) {
                        int t = p[a];
                        p[a] = p[b];
                        p[b] = t;
                        for (++a, b = p.length - 1; a < b; ++a, --b) {
                            t = p[a];
                            p[a] = p[b];
                            p[b] = t;
                        }
                        return true;
                    }
        return false;
    }

    <T> List<T>[] createAdjacencyList(int countVertex) {
        List<T>[] res = new List[countVertex];
        for (int i = 0; i < countVertex; i++) {
            res[i] = new ArrayList<T>();
        }
        return res;
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

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

    static class TaskD {
        final int maxv = (int) 1E6;
        int[] a = new int[2 * maxv + 1];

        public void solve(int testNumber, InputReader reader, OutputWriter writer) {
            int n = reader.readInteger();
            for (int i = 0; i < n; i++) {
                int x = reader.readInteger();
                a[x] = x;
            }

            for (int i = 1; i <= 2 * maxv; i++) {
                if (a[i] != i) {
                    a[i] = a[i - 1];
                }
            }

            int ans = 0;
            for (int i = 1; i <= maxv; i++) {
                if (a[i] == i) for (int j = i * 2 - 1; j <= 2 * maxv; j += i) {
                    ans = Math.max(ans, a[j] % i);
                }
            }
            writer.print(ans);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.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 int readInteger() {
            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 boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void close() {
            writer.flush();
            writer.close();
        }

    }
}


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

public class D {

	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt(), MAX = (int)2e6+5;
		int[] pre = new int[MAX];
		int[] a = new int[n];
		for(int i = 0; i < n; i++)
			pre[(a[i] = sc.nextInt()) + 1] = a[i];
		for(int i = 1; i < MAX; i++)
			if(pre[i] == 0)
				pre[i] = pre[i-1];
		int max = 0;
		boolean[] taken = new boolean[MAX]; taken[1] = true;
		for(int i = 0; i < n; i++)
		{
			if(taken[a[i]]) continue;
			taken[a[i]] = true;
			for(int j = a[i] * 2; j < MAX; j+= a[i])
				if(a[i] < pre[j])
					max = Math.max(max, pre[j] % a[i]);
		}
		out.println(max);
		out.flush();
		out.close();
	}

	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream System){	br = new BufferedReader(new InputStreamReader(System));	}
		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public String nextLine()throws IOException{return br.readLine();}
		public int nextInt() throws IOException {return Integer.parseInt(next());}
		public double nextDouble() throws IOException {return Double.parseDouble(next());}
		public char nextChar()throws IOException{return next().charAt(0);}
		public Long nextLong()throws IOException{return Long.parseLong(next());}
		public boolean ready() throws IOException{return br.ready();}
		public void waitForInput(){for(long i = 0; i < 3e9; i++);}
	}
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;

public class CF_276D {

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);

        HashSet<Integer> h = new HashSet<Integer>();

        int n = sc.nextInt();

        for(int i = 0; i < n; i++)
            h.add(sc.nextInt());

        Integer[] nums = new Integer[h.size()];
        nums = h.toArray(nums);

        Arrays.sort(nums);

        int best = 0;

        for(int i=n-1; i>=0; i--){
            if (nums[i]<=best)
                break;
            for(int j =2; (j-1)*nums[i]-1 <= nums[nums.length-1];j++){
                int ind = Arrays.binarySearch(nums, j*nums[i]-1);
                if(ind>=0){
                    best = Math.max(best,nums[ind]%nums[i]);
                } else {
                    ind = -ind - 2;
                    best = Math.max(best,nums[ind]%nums[i]);
                }
            }
        }

        System.out.println(best);

    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

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

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

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

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

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

}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;

public class CF_276D {

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);

        HashSet<Integer> h = new HashSet<Integer>();

        int n = sc.nextInt();

        for(int i = 0; i < n; i++)
            h.add(sc.nextInt());

        Integer[] nums = new Integer[h.size()];
        nums = h.toArray(nums);

        Arrays.sort(nums);

        int best = 0;

        for(int i=n-1; i>=0; i--){
            if(nums[i]==1)
                continue;
            if (nums[i]<=best)
                break;
            for(int j =2; (j-1)*nums[i]-1 <= nums[nums.length-1];j++){
                int ind = Arrays.binarySearch(nums, j*nums[i]-1);
                if(ind>=0){
                    best = Math.max(best,nums[ind]%nums[i]);
                } else {
                    ind = -ind - 2;
                    best = Math.max(best,nums[ind]%nums[i]);
                }
            }
        }

        System.out.println(best);

    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

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

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

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

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

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

}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;

public class CF_276D {

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);

        HashSet<Integer> h = new HashSet<Integer>();

        int n = sc.nextInt();

        for(int i = 0; i < n; i++)
            h.add(sc.nextInt());

        Integer[] nums = new Integer[h.size()];
        nums = h.toArray(nums);

        Arrays.sort(nums);

        int best = 0;

        for(int i=n-1; i>=0; i--){
            if (nums[i]<=best)
                break;
            for(int j =2; j*nums[i]-1 <= nums[nums.length-1];j++){
                int ind = Arrays.binarySearch(nums, j*nums[i]-1);
                if(ind>=0){
                    best = Math.max(best,nums[ind]%nums[i]);
                } else {
                    ind = -ind - 1;
                    best = Math.max(best,nums[ind]%nums[i]);
                }
            }
        }

        System.out.println(best);

    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

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

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

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

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

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

}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;

public class CF_276D {

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);

        HashSet<Integer> h = new HashSet<Integer>();

        int n = sc.nextInt();

        for(int i = 0; i < n; i++)
            h.add(sc.nextInt());

        n = h.size();
        Integer[] nums = new Integer[n];
        nums = h.toArray(nums);

        Arrays.sort(nums);

        int best = 0;

        for(int i=n-1; i>=0; i--){
            if(nums[i]==1)
                continue;
            if (nums[i]<=best)
                break;
            for(int j =2; (j-1)*nums[i]-1 <= nums[nums.length-1];j++){
                int ind = Arrays.binarySearch(nums, j*nums[i]-1);
                if(ind>=0){
                    best = Math.max(best,nums[ind]%nums[i]);
                } else {
                    ind = -ind - 2;
                    best = Math.max(best,nums[ind]%nums[i]);
                }
            }
        }

        System.out.println(best);

    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

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

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

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

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

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

}


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

/**
 * Created by zgmnkv on 12.11.2014.
 */
public class Task485D {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        int n = Integer.parseInt(in.readLine());
        String[] parts = in.readLine().split(" ");

        int[] a = new int[n];
        for (int i = 0; i < n; ++i) {
            a[i] = Integer.parseInt(parts[i]);
        }

        Arrays.sort(a);

        int max = a[a.length - 1];
        int[] m = new int[2 * max];
        for (int i = 0; i <= a[0]; ++i) {
            m[i] = -1;
        }
        for (int i = 1; i < n; ++i) {
            for (int j = a[i-1] + 1; j <= a[i]; ++j) {
                m[j] = i;
            }
        }
        for (int i = max + 1; i < 2 * max; ++i) {
            m[i] = a.length - 1;
        }

        int res = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] == 1) {
                continue;
            }
            if (i > 0 && a[i] == a[i - 1]) {
                continue;
            }
            for (int k = 2; k < 2 * max / a[i]; ++k) {
                int q = a[m[k * a[i]]] % a[i];
                if (q > res) {
                    res = q;
                }
            }
        }

        out.println(res);

        in.close();
        out.close();
    }
}


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

/**
 * Created by zgmnkv on 12.11.2014.
 */
public class Task485D {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        int n = Integer.parseInt(in.readLine());
        String[] parts = in.readLine().split(" ");

        int[] a = new int[n];
        for (int i = 0; i < n; ++i) {
            a[i] = Integer.parseInt(parts[i]);
        }

        int max = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (a[i] >= a[j]) {
                    int m = a[i] % a[j];
                    if (m > max) {
                        max = m;
                    }
                }
            }
        }

        out.println(max);

        in.close();
        out.close();
    }
}


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

/**
 * Created by zgmnkv on 12.11.2014.
 */
public class Task485D {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        int n = Integer.parseInt(in.readLine());
        String[] parts = in.readLine().split(" ");

        int[] a = new int[n];
        for (int i = 0; i < n; ++i) {
            a[i] = Integer.parseInt(parts[i]);
        }

        Arrays.sort(a);

        int max = a[a.length - 1];
        int[] m = new int[2 * max + 1];
        for (int i = 0; i <= a[0]; ++i) {
            m[i] = -1;
        }
        for (int i = 1; i < n; ++i) {
            for (int j = a[i-1] + 1; j <= a[i]; ++j) {
                m[j] = i - 1;
            }
        }
        for (int i = max + 1; i < 2 * max + 1; ++i) {
            m[i] = a.length - 1;
        }

        int res = 0;
        for (int i = 0; i < n; ++i) {
            if (a[i] == 1) {
                continue;
            }
            if (i > 0 && a[i] == a[i - 1]) {
                continue;
            }
            for (int k = 2; k <= 2 * max / a[i]; ++k) {
                int q = a[m[k * a[i]]] % a[i];
                if (q > res) {
                    res = q;
                }
            }
        }

        out.println(res);

        in.close();
        out.close();
    }
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class Main
	{
		static BufferedReader	in	= new BufferedReader(new InputStreamReader(
											System.in));
		static BufferedWriter	out	= new BufferedWriter(
											new OutputStreamWriter(System.out));
		final static int		MOD	= 1000000007;
		static double			start;

		public static void main(String[] args) throws Exception
			{

				long test = 1;
				// long test = strToLong(in.readLine().trim()); // if number of
				// test cases
				for (int i = 0; i < test; i++)
					{
						req();

					}

				out.flush();
				// System.out.println(System.currentTimeMillis() - start +
				// " **");

				out.close();

			}

		public static void req() throws Exception
			{

				int MAX_NUM = 1000001;
				int n = strToInt(in.readLine());
				long arr[] = splits(in.readLine(), n);

				int count = 0;
				boolean hash[] = new boolean[MAX_NUM];
				for (long x : arr)
					{
						int temp = (int) x;
						if (!hash[temp])
							{
								count++;
								hash[temp] = true;
							}
					}

				long a[] = new long[count];
				int idx = 0;
				for(int i =0;i<MAX_NUM;i++)
					{
						if(hash[i])
							{
								a[idx++] = i;
							}
					}

				int hash2[] = new int[2* MAX_NUM];
				int pre = -1;
				hash2[0] = -1;
				for(int i =1;i<2*MAX_NUM;i++)
					{
						hash2[i] = pre;
						if(i<MAX_NUM && hash[i])
							{

								pre = i;
							}

					}

				long ans =0;
				for(int j =0;j<count;j++)
					{
						int aj = (int)a[j];

						for(int x = 2*aj; x<=a[count-1] + aj;x += aj)
							{

								   long ai = hash2[x];
										long temp = ai%aj;
										if(temp>ans)
											ans=temp;

							}
					}

				out.write(ans+"\n");

			}

		// takes a string with spaces as input and produces an array of long of
		// length n
		// input should be valid
		// should know length of array beforehand
		static long[] splits(String s, int n)
			{

				long[] out = new long[n];
				int idx = 0;

				StringTokenizer ss = new StringTokenizer(s);
				while (ss.hasMoreTokens())
					{

						out[idx] = strToLong(ss.nextToken());

						idx++;
					}

				return out;

			}

		// like split but first integer in input is nuumber of integer that
		// follows
		static long[] splits2(String s)
			{

				long[] out;
				int idx = 0;

				StringTokenizer ss = new StringTokenizer(s);
				int n = strToInt(ss.nextToken());
				out = new long[n];
				while (ss.hasMoreTokens())
					{
						out[idx] = strToLong(ss.nextToken());

						idx++;
					}

				return out;

			}

		// returns gcd of two long parameters
		// i think its algo by euclid
		static long gcd(long a, long b)
			{

				return b == 0 ? a : gcd(b, a % b);
			}

		// returns maximum among two long
		public static long max(long a, long b)
			{
				return a > b ? a : b;
			}

		// modulus of product of two long parameter
		public static long modPro(long a, long b)
			{
				// check value of MOD , may vary for different questions
				// or change the function to take mod as an input parameter
				// tongue emoticon
				return ((a % MOD) * (b % MOD)) % MOD;
			}

		public static long modInv(long a)
			{
				return new BigInteger(a + "").modPow(
						new BigInteger((MOD - 2) + ""),
						new BigInteger(MOD + "")).longValue();
			}

		// returns minimum among two long
		public static long min(long a, long b)
			{
				return a < b ? a : b;
			}

		// take a string as input and convert it to an integer
		// takes care of negatives
		// no commas or space expected in string
		// copied from codechef
		public static int strToInt(String str)
			{
				int i = 0;
				int num = 0;
				boolean isNeg = false;
				// Check for negative sign; if it's there, set the isNeg flag
				if (str.charAt(0) == '-')
					{
						isNeg = true;
						i = 1;
					}
				// Process each character of the string;
				while (i < str.length())
					{
						num *= 10;
						num += str.charAt(i++) - '0'; // Minus the ASCII code of
														// '0' to get
						// the value of the charAt(i++).
					}
				if (isNeg)
					num = -num;
				return num;
			}

		// take a string as input and convert it to a long
		// takes care of negatives
		// no commas or space expected in string
		// copied from codechef
		public static long strToLong(String str)
			{
				int i = 0;
				long num = 0;
				boolean isNeg = false;
				// Check for negative sign; if it's there, set the isNeg flag
				if (str.charAt(0) == '-')
					{
						isNeg = true;
						i = 1;
					}
				// Process each character of the string;
				while (i < str.length())
					{
						num *= 10;
						num += str.charAt(i++) - '0'; // Minus the ASCII code of
														// '0' to get
						// the value of the charAt(i++).
					}
				if (isNeg)
					num = -num;
				return num;
			}

	}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class Main
	{
		static BufferedReader	in	= new BufferedReader(new InputStreamReader(
											System.in));
		static BufferedWriter	out	= new BufferedWriter(
											new OutputStreamWriter(System.out));
		final static int		MOD	= 1000000007;
		static double			start;

		public static void main(String[] args) throws Exception
			{

				long test = 1;
				// long test = strToLong(in.readLine().trim()); // if number of
				// test cases
				for (int i = 0; i < test; i++)
					{
						req();

					}

				out.flush();
				// System.out.println(System.currentTimeMillis() - start +
				// " **");

				out.close();

			}

		public static void req() throws Exception
			{

				int MAX_NUM = 1000001;
				int n = strToInt(in.readLine());
				long arr[] = splits(in.readLine(), n);

				int count = 0;
				boolean hash[] = new boolean[MAX_NUM];
				for (long x : arr)
					{
						int temp = (int) x;
						if (!hash[temp])
							{
								count++;
								hash[temp] = true;
							}
					}

				long a[] = new long[count];
				int idx = 0;
				for(int i =0;i<MAX_NUM;i++)
					{
						if(hash[i])
							{
								a[idx++] = i;
							}
					}

				int hash2[] = new int[2* MAX_NUM];
				int pre = -1;
				hash2[0] = -1;
				for(int i =1;i<MAX_NUM;i++)
					{
						if(hash[i])
							{
								hash2[i] = pre;
								pre = i;
							}
						else
							hash2[i] = pre;

					}

				long ans =0;
				for(int j =0;j<count;j++)
					{
						int aj = (int)a[j];

						for(int x = 2*aj; x<=a[count-1] + aj;x += aj)
							{

								   long ai = hash2[x];
										long temp = ai%aj;
										if(temp>ans)
											ans=temp;

							}
					}

				out.write(ans+"\n");

			}

		// takes a string with spaces as input and produces an array of long of
		// length n
		// input should be valid
		// should know length of array beforehand
		static long[] splits(String s, int n)
			{

				long[] out = new long[n];
				int idx = 0;

				StringTokenizer ss = new StringTokenizer(s);
				while (ss.hasMoreTokens())
					{

						out[idx] = strToLong(ss.nextToken());

						idx++;
					}

				return out;

			}

		// like split but first integer in input is nuumber of integer that
		// follows
		static long[] splits2(String s)
			{

				long[] out;
				int idx = 0;

				StringTokenizer ss = new StringTokenizer(s);
				int n = strToInt(ss.nextToken());
				out = new long[n];
				while (ss.hasMoreTokens())
					{
						out[idx] = strToLong(ss.nextToken());

						idx++;
					}

				return out;

			}

		// returns gcd of two long parameters
		// i think its algo by euclid
		static long gcd(long a, long b)
			{

				return b == 0 ? a : gcd(b, a % b);
			}

		// returns maximum among two long
		public static long max(long a, long b)
			{
				return a > b ? a : b;
			}

		// modulus of product of two long parameter
		public static long modPro(long a, long b)
			{
				// check value of MOD , may vary for different questions
				// or change the function to take mod as an input parameter
				// tongue emoticon
				return ((a % MOD) * (b % MOD)) % MOD;
			}

		public static long modInv(long a)
			{
				return new BigInteger(a + "").modPow(
						new BigInteger((MOD - 2) + ""),
						new BigInteger(MOD + "")).longValue();
			}

		// returns minimum among two long
		public static long min(long a, long b)
			{
				return a < b ? a : b;
			}

		// take a string as input and convert it to an integer
		// takes care of negatives
		// no commas or space expected in string
		// copied from codechef
		public static int strToInt(String str)
			{
				int i = 0;
				int num = 0;
				boolean isNeg = false;
				// Check for negative sign; if it's there, set the isNeg flag
				if (str.charAt(0) == '-')
					{
						isNeg = true;
						i = 1;
					}
				// Process each character of the string;
				while (i < str.length())
					{
						num *= 10;
						num += str.charAt(i++) - '0'; // Minus the ASCII code of
														// '0' to get
						// the value of the charAt(i++).
					}
				if (isNeg)
					num = -num;
				return num;
			}

		// take a string as input and convert it to a long
		// takes care of negatives
		// no commas or space expected in string
		// copied from codechef
		public static long strToLong(String str)
			{
				int i = 0;
				long num = 0;
				boolean isNeg = false;
				// Check for negative sign; if it's there, set the isNeg flag
				if (str.charAt(0) == '-')
					{
						isNeg = true;
						i = 1;
					}
				// Process each character of the string;
				while (i < str.length())
					{
						num *= 10;
						num += str.charAt(i++) - '0'; // Minus the ASCII code of
														// '0' to get
						// the value of the charAt(i++).
					}
				if (isNeg)
					num = -num;
				return num;
			}

	}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class Main
	{
		static BufferedReader	in	= new BufferedReader(new InputStreamReader(
											System.in));
		static BufferedWriter	out	= new BufferedWriter(
											new OutputStreamWriter(System.out));
		final static int		MOD	= 1000000007;
		static double			start;

		public static void main(String[] args) throws Exception
			{

				long test = 1;
				// long test = strToLong(in.readLine().trim()); // if number of
				// test cases
				for (int i = 0; i < test; i++)
					{
						req();

					}

				out.flush();
				// System.out.println(System.currentTimeMillis() - start +
				// " **");

				out.close();

			}

		public static void req() throws Exception
			{

				int MAX_NUM = 1000001;
				int n = strToInt(in.readLine());
				long arr[] = splits(in.readLine(), n);

				int count = 0;
				boolean hash[] = new boolean[MAX_NUM];
				for (long x : arr)
					{
						int temp = (int) x;
						if (!hash[temp])
							{
								count++;
								hash[temp] = true;
							}
					}

				long a[] = new long[count];
				int idx = 0;
				for(int i =0;i<MAX_NUM;i++)
					{
						if(hash[i])
							{
								a[idx++] = i;
							}
					}

				long ans =0;
				for(int j =0;j<count;j++)
					{
						int aj = (int)a[j];

						for(int x = 2*aj; x<=a[count-1] + aj;x += aj)
							{
								idx = Arrays.binarySearch(a, x);
								if(idx<0)
									{
										// idx is -insertion point -1
										idx*=-1;
										idx--;

									}
								idx--;
								if(idx>0)
									{
										long temp = a[idx]%aj;
										if(temp>ans)
											ans=temp;
									}

							}
					}

				out.write(ans+"\n");

			}

		// takes a string with spaces as input and produces an array of long of
		// length n
		// input should be valid
		// should know length of array beforehand
		static long[] splits(String s, int n)
			{

				long[] out = new long[n];
				int idx = 0;

				StringTokenizer ss = new StringTokenizer(s);
				while (ss.hasMoreTokens())
					{

						out[idx] = strToLong(ss.nextToken());

						idx++;
					}

				return out;

			}

		// like split but first integer in input is nuumber of integer that
		// follows
		static long[] splits2(String s)
			{

				long[] out;
				int idx = 0;

				StringTokenizer ss = new StringTokenizer(s);
				int n = strToInt(ss.nextToken());
				out = new long[n];
				while (ss.hasMoreTokens())
					{
						out[idx] = strToLong(ss.nextToken());

						idx++;
					}

				return out;

			}

		// returns gcd of two long parameters
		// i think its algo by euclid
		static long gcd(long a, long b)
			{

				return b == 0 ? a : gcd(b, a % b);
			}

		// returns maximum among two long
		public static long max(long a, long b)
			{
				return a > b ? a : b;
			}

		// modulus of product of two long parameter
		public static long modPro(long a, long b)
			{
				// check value of MOD , may vary for different questions
				// or change the function to take mod as an input parameter
				// tongue emoticon
				return ((a % MOD) * (b % MOD)) % MOD;
			}

		public static long modInv(long a)
			{
				return new BigInteger(a + "").modPow(
						new BigInteger((MOD - 2) + ""),
						new BigInteger(MOD + "")).longValue();
			}

		// returns minimum among two long
		public static long min(long a, long b)
			{
				return a < b ? a : b;
			}

		// take a string as input and convert it to an integer
		// takes care of negatives
		// no commas or space expected in string
		// copied from codechef
		public static int strToInt(String str)
			{
				int i = 0;
				int num = 0;
				boolean isNeg = false;
				// Check for negative sign; if it's there, set the isNeg flag
				if (str.charAt(0) == '-')
					{
						isNeg = true;
						i = 1;
					}
				// Process each character of the string;
				while (i < str.length())
					{
						num *= 10;
						num += str.charAt(i++) - '0'; // Minus the ASCII code of
														// '0' to get
						// the value of the charAt(i++).
					}
				if (isNeg)
					num = -num;
				return num;
			}

		// take a string as input and convert it to a long
		// takes care of negatives
		// no commas or space expected in string
		// copied from codechef
		public static long strToLong(String str)
			{
				int i = 0;
				long num = 0;
				boolean isNeg = false;
				// Check for negative sign; if it's there, set the isNeg flag
				if (str.charAt(0) == '-')
					{
						isNeg = true;
						i = 1;
					}
				// Process each character of the string;
				while (i < str.length())
					{
						num *= 10;
						num += str.charAt(i++) - '0'; // Minus the ASCII code of
														// '0' to get
						// the value of the charAt(i++).
					}
				if (isNeg)
					num = -num;
				return num;
			}

	}


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

public class MaximumValue {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int n = Integer.parseInt(br.readLine());
        int[] num = new int[n + 2];
        st = new StringTokenizer(br.readLine());
        for (int i = 1; i <= n; i++) {
            num[i] = Integer.parseInt(st.nextToken());
        }
        num[n + 1] = 2000001;
        Arrays.sort(num);

        int[] maxTill = new int[2000001];
        for (int i = 1; i <= n; i++) {
            if (num[i] == num[i - 1]) {
                continue;
            }
            for (int j = num[i]; j < num[i + 1]; j++) {
                maxTill[j] = num[i];
            }
        }

        int max = 0;
        int l = num[n] << 1;
        for (int i = 1; i <= n; i++) {
            if (num[i] == num[i - 1]) {
                continue;
            }

            for (int j = num[i] << 1; j <= l; j += num[i]) {
                if (maxTill[j] > num[i]) {
                    max = Math.max(max, maxTill[j] % num[i]);
                }
            }
        }
        System.out.println(max);
    }
}


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

public class MaximumValue {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int n = Integer.parseInt(br.readLine());
        int[] num = new int[n + 2];
        st = new StringTokenizer(br.readLine());
        for (int i = 1; i <= n; i++) {
            num[i] = Integer.parseInt(st.nextToken());
        }
        num[n + 1] = 2000001;
        Arrays.sort(num);

        int[] maxTill = new int[2000001];
        for (int i = 1; i <= n; i++) {
            if (num[i] == num[i - 1]) {
                continue;
            }
            for (int j = num[i]; j < num[i + 1]; j++) {
                maxTill[j] = num[i];
            }
        }

        int max = 0;
        int l = num[n] << 1;
        for (int i = 1; i <= n; i++) {
            if (num[i] == num[i - 1]) {
                continue;
            }

            for (int j = num[i] << 1; j <= l; j += num[i]) {
                if (maxTill[j - 1] > num[i]) {
                    max = Math.max(max, maxTill[j - 1] % num[i]);
                }
            }
        }
        System.out.println(max);
    }
}


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

public class MaximumValue {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int n = Integer.parseInt(br.readLine());
        int[] num = new int[2000001];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            num[Integer.parseInt(st.nextToken())] = 1;
        }

        int[] maxTill = new int[2000001];
        for (int i = 1; i < 2000001; i++) {
            maxTill[i] = num[i] == 1 ? i : maxTill[i - 1];
        }

        int max = 0;
        for (int i = 1; i < 2000001; i++) {
            if (num[i] == 0) {
                continue;
            }

            for (int j = i << 1; j < 2000001; j += i) {
                if (maxTill[j - 1] > i) {
                    max = Math.max(max, maxTill[j - 1] % i);
                }
            }
        }
        System.out.println(max);
    }
}


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<max;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 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<max;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];
		for(int i=arr[0];i<max;i++) {
			helper[i]=bs_2(arr,i);
		}
		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 &&arr[pos]==i) {
				helper[i]=i;
				pos++;
			}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;
		}
	}

}