#include <bits/stdc++.h>
using namespace std;

#define maxn 100005
typedef long long ll;
typedef double db;

ll n;
ll a[maxn];
ll b[maxn];

ll v1[maxn];
ll v2[maxn];
ll isrecord[maxn];

//ojo con n

void add(ll where, ll what, ll v[]) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where, ll v[]) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to, ll v[]) {
	return query(to, v) - query(from - 1, v);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		if(i)
			b[a[i]] = query(1, a[i] - 1, v1);
		if(b[a[i]] == i)
			isrecord[a[i]] = 1;
		add(a[i], 1, v1);
	}

	int val = -2;
	int ans = n;
	for (int i = n - 1; i >= 0; i--) {
		ll tmp = query(1, a[i] - 1, v2) - isrecord[a[i]];
		if(tmp > val || (tmp == val && a[i] < ans))
		{
			val = tmp;
			ans = a[i];
		}
		if(b[a[i]] == i - 1)
			add(a[i], 1, v2);
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define maxn 100005
typedef long long ll;
typedef double db;

int n;
int a[maxn];

ll v[maxn];
ll v1[maxn];

//ojo con n

void add(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v[where] += what;
	}
}

ll query(ll where) {
	ll sum = v[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v[where];
	}
	return sum;
}

ll query(ll from, ll to) {
	return query(to) - query(from - 1);
}

void add2(ll where, ll what) {
	for (where++; where <= n; where += where & -where) {
		v1[where] += what;
	}
}

ll query2(ll where) {
	ll sum = v1[0];
	for (where++; where > 0; where -= where & -where) {
		sum += v1[where];
	}
	return sum;
}

ll query2(ll from, ll to) {
	return query2(to) - query2(from - 1);
}

int bi[maxn];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	if (n < 3) {
		cout << 1;
		return 0;
	}

	for (int i = 0; i < n; ++i) {
		if (i)
			bi[a[i]] = query(1, a[i] - 1);
		add(a[i], 1);
	}

	int big = 0;
	int val = 1;
	for (int i = n - 1; i >= 0; i--) {
		if (i < n - 1) {
			if (query2(1, a[i] - 1) > big) {
				big = query2(1, a[i] - 1);
				val = a[i];
			}
			else if (query2(1, a[i] - 1) == big && a[i] < val)
				val = a[i];
		}
		if (bi[a[i]] == i - 1)
			add2(a[i], 1);
	}

	cout << val;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
int data[200010];
int main()
{
    int n,res,i,j,pos;
    while(~scanf("%d",&n))
    {
        res = 0;
        for(i=0; i<n; i++)
            scanf("%d",&data[i]);
        sort(data, data+n);
        n =int(unique(data,data+n)-data);
        for(i=0; i<n; i++)
        {
            for(j=data[i]*2; j<=data[n-1]; j+=data[i])
            {
                pos =int(lower_bound(data, data+n, j) - data);
                if(pos)
                    res =max (res,data[pos-1]%data[i]);
            }
                res = max(res,data[n-1]%data[i]);
        }
        printf("%d\n",res);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
int a[1000005];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        sort(a, a+n);
        n =int(unique(a,a+n)-a);
        int ret = 0;
        for(int i=0; i<n; i++)
        {
            for(int j=(a[i]<<1); j<=a[n-1]; j+=a[i])
            {
                int tmp =int(lower_bound(a, a+n, j) - a);
                if(tmp && ret<(a[tmp-1]%a[i]))
                    ret = (a[tmp-1]%a[i]);
            }
            if(ret < (a[n-1]%a[i]))
                ret = (a[n-1]%a[i]);
        }
        printf("%d\n",ret);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define inf 0x3f3f3f
#define ll long long
const ll mod=1e9+7;

int p[200020];
int a[1000020];
int b[1000010];
int main()
{
    int i,j,k;
    int n;
    scanf("%d",&n);
    memset(a,0,sizeof(a));
    for(i=0;i<n;i++)
    {
        scanf("%d",&p[i]);
        a[p[i]]=1;
    }
    int t=-1;
    for(i=1;i<=1000000;i++)
    {
        b[i]=t;
        if(a[i]==1)
            t=i;
    }
    sort(p,p+n);
    int ans=0;
    for(i=0;i<n;i++)
    {
        if(i!=0 && p[i]==p[i-1]) continue;
        if(p[i]==1) continue;
        for(j=2;;j++)
        {
            int h=j*p[i];
            if(h>1000000)
            {
                if(p[n-1]>=p[i])
                    ans=max(ans,p[n-1]%p[i]);
                break;
            }
            ans=max(ans,b[h]%p[i]);
        }
    }
    cout<<ans<<endl;
    //system("pause");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define inf 0x3f3f3f
#define ll long long
const ll mod=1e9+7;

int p[200020];
int a[1000020];
int b[1000010];
int main()
{
	int i,j,k;
	int n;
	scanf("%d",&n);
	memset(a,0,sizeof(a));
	for(i=0;i<n;i++)
	{
		scanf("%d",&p[i]);
		a[p[i]]=1;
	}
	int t=-1;
	for(i=1;i<=1000000;i++)
	{
		b[i]=t;
		if(a[i]==1)
			t=i;
	}
	sort(p,p+n);
	int ans=0;
	for(i=0;i<n;i++)
	{
		if(i!=0 && p[i]==p[i-1]) continue;
		if(p[i]==1) continue;
		for(j=2;;j++)
		{
			int h=j*p[i];
			if(h>1000000)
			{
				if(p[n-1]>=p[i])
					ans=max(ans,p[n-1]%p[i]);
				break;
			}
			ans=max(ans,a[h]%p[i]);
		}
	}
	cout<<ans<<endl;
	//system("pause");
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,j=2,ans=0,k=0,id=0,a[223456]; cin >> n;for (int i = 0; i<n; i++)cin >> a[i];
    sort(a, a + n);
    n = unique(a, a + n) - a;
    for (int i = n - 1; i >= 0; i--)
    while (1){
         k = a[i] / j + 1;
        if (k <= ans + 1)break;
        int id = lower_bound(a, a + n, k) - a;
        ans = max(ans, a[i] % a[id]);
        j++;
    }
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,ans=0,k=0,id=0,a[223456]; cin >> n;for (int i = 0; i<n; i++)cin >> a[i];
    sort(a, a + n);
    n = unique(a, a + n) - a;
    for (int i = n - 1; i >= 0; i--)
        for (int j = 2; k <= ans + 1;j++)
        {
            k = a[i] / j + 1; if (k <= ans + 1)break;
            id = lower_bound(a, a + n, k) - a;
            ans = max(ans, a[i] % a[id]);
        }
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,j=2,ans=0,k=0,id=0,a[223456]; cin >> n;for (int i = 0; i<n; i++)cin >> a[i];
    sort(a, a + n);
    n = unique(a, a + n) - a;
    for (int i = n - 1; i >= 0; i--)
    {
        j = 2;
        while (1)
        {
            k = a[i] / j + 1;
            if (k <= ans + 1)break;
            id = lower_bound(a, a + n, k) - a;
            ans = max(ans, a[i] % a[id]);
            j++;
        }
    }
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,j=2,ans=0,k=0,id=0,a[223456]; cin >> n;for (int i = 0; i<n; i++)cin >> a[i];
    sort(a, a + n);
    n = unique(a, a + n) - a;
    for (int i = n - 1; i >= 0; i--)
    while (1)
    {
         k = a[i] / j + 1;
        if (k <= ans + 1)break;
        id = lower_bound(a, a + n, k) - a;
        ans = max(ans, a[i] % a[id]);
        j++;
    }
    cout << ans << endl;
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <algorithm>
#include <assert.h>

typedef long long ll;
#define mp make_pair
#define mt(a, b, c) mp(a, mp(b, c))

using namespace std;

int main()
{

	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);

	set<int> S;
	for (int i = 0; i < a.size(); i++)
		S.insert(a[i]);
	a.clear();
	for (set<int>::iterator i = S.begin(); i != S.end(); i++)
		a.push_back(*i);

	int ans = 0;

	for (int i = 0; i < a.size(); i++)
	{
		for (int j = a[i] * 2; j < 2e5 + 100; j += a[i])
		{
			ans = max(ans, (*(lower_bound(a.begin(), a.end(), j - 1) - 1)) % a[i]);
		}
	}

	printf("%d", ans);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <algorithm>
#include <assert.h>

typedef long long ll;
#define mp make_pair
#define mt(a, b, c) mp(a, mp(b, c))

using namespace std;

int main()
{

	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);

	set<int> S;
	for (int i = 0; i < a.size(); i++)
		S.insert(a[i]);
	a.clear();
	for (set<int>::iterator i = S.begin(); i != S.end(); i++)
		a.push_back(*i);

	int ans = 0;

	for (int i = 0; i < a.size(); i++)
	{
		for (int j = a[i] * 2; j < 2e5 + 100; j += a[i])
		{
			ans = max(ans, (*(lower_bound(a.begin(), a.end(), j) - 1)) % a[i]);
		}
	}

	printf("%d", ans);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <algorithm>
#include <assert.h>

typedef long long ll;
#define mp make_pair
#define mt(a, b, c) mp(a, mp(b, c))

using namespace std;

int main()
{

	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);

	sort(a.begin(), a.end());
	a.resize(unique(a.begin(), a.end()) - a.begin());

	int ans = 0;

	for (int i = 0; i < a.size(); i++)
	{
		for (int j = a[i] * 2; j < 2e6 + 100; j += a[i])
		{
			ans = max(ans, (*(lower_bound(a.begin(), a.end(), j) - 1)) % a[i]);
		}
	}

	printf("%d", ans);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <algorithm>
#include <assert.h>

typedef long long ll;
#define mp make_pair
#define mt(a, b, c) mp(a, mp(b, c))

using namespace std;

int main()
{

	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);

	set<int> S;
	for (int i = 0; i < a.size(); i++)
		S.insert(a[i]);
	a.clear();
	for (set<int>::iterator i = S.begin(); i != S.end(); i++)
		a.push_back(*i);

	int ans = 0;

	for (int i = 0; i < a.size(); i++)
	{
		for (int j = a[i] * 2; j < 1e5 + 100; j += a[i])
		{
			ans = max(ans, (*(lower_bound(a.begin(), a.end(), j) - 1)) % a[i]);
		}
	}

	printf("%d", ans);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <algorithm>
#include <assert.h>

typedef long long ll;
#define mp make_pair
#define mt(a, b, c) mp(a, mp(b, c))

using namespace std;

int main()
{

	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);

	set<int> S;
	for (int i = 0; i < a.size(); i++)
		S.insert(a[i]);
	a.clear();
	for (set<int>::iterator i = S.begin(); i != S.end(); i++)
		a.push_back(*i);

	int ans = 0;

	for (int i = 0; i < a.size(); i++)
	{
		for (int j = a[i] * 2; j < 1e5 + 100; j += a[i])
		{
			ans = max(ans, (*(lower_bound(a.begin(), a.end(), j - 1) - 1)) % a[i]);
		}
	}

	printf("%d", ans);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <algorithm>
#include <assert.h>

typedef long long ll;
#define mp make_pair
#define mt(a, b, c) mp(a, mp(b, c))

using namespace std;

int main()
{

	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);

	set<int> S;
	for (int i = 0; i < a.size(); i++)
		S.insert(a[i]);
	a.clear();
	for (set<int>::iterator i = S.begin(); i != S.end(); i++)
		a.push_back(*i);

	int ans = 0;

	for (int i = 0; i < a.size(); i++)
	{
		for (int j = a[i] * 2; j < 2e6 + 100; j += a[i])
		{
			ans = max(ans, (*(lower_bound(a.begin(), a.end(), j) - 1)) % a[i]);
		}
	}

	printf("%d", ans);

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <array>
#define MAXM 100000
#define MAXN 100000
#define ll long long

using namespace std;

int a[111111];
int counts[1111111];

int main() {
    srand(time(NULL));
    int n;
    cin>>n;
    for(int i=0;i<=1000000;i++)
        counts[i]=0;
    for(int i=1;i<=n;i++) {
        cin>>a[i];
        //a[i]=rand()%1000000+1;
        counts[a[i]]++;
    }

    for(int i=1;i<=1000000;i++) {
        counts[i]=counts[i-1]+counts[i];
    }

    int answer = 0;
    for(int i=1;i<=n;i++) {
        int x = a[i];
        for(int j=1;j*x<=1000000;j++) {
            int L = j*x;
            int H = min(1000000, (j+1)*x-1);

            if (counts[L]==counts[H])
                continue;

            int target = counts[H];
            int low = L;
            int high = H;
            while(low < high) {
                int mid = (low + high + 1)/2;
                if(counts[mid]>=counts[H]) {
                    high = mid - 1;
                } else {
                    low = mid;
                }
            }
            answer = max(answer, (low+1) % x);

        }
    }

    cout << answer << endl;

}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <array>
#define MAXM 100000
#define MAXN 100000
#define ll long long

using namespace std;

int a[111111];
int counts[1111111];

int main() {
    int n;
    cin>>n;
    for(int i=0;i<=1000000;i++)
        counts[i]=0;
    for(int i=1;i<=n;i++) {
        cin>>a[i];
        //a[i]=rand()%1000000+1;
        counts[a[i]]++;
    }

    for(int i=1;i<=1000000;i++) {
        counts[i]=counts[i-1]+counts[i];
    }

    int answer = 0;
    for(int i=1;i<=n;i++) {
        int x = a[i];
        for(int j=1;j*x<=1000000;j++) {
            int L = j*x;
            int H = min(1000000, (j+1)*x-1);

            if (counts[L]==counts[H])
                continue;

            int target = counts[H];
            int low = L;
            int high = H;
            while(low < high) {
                int mid = (low + high + 1)/2;
                if(counts[mid]>=counts[H]) {
                    high = mid - 1;
                } else {
                    low = mid;
                }
            }
            answer = max(answer, (low+1) % x);

        }
    }

    cout << answer << endl;

}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <array>
#define MAXM 100000
#define MAXN 100000
#define ll long long

using namespace std;

int a[222222];
int counts[1111111];

int main() {
    int n;
    cin>>n;
    for(int i=0;i<=1000000;i++)
        counts[i]=0;
    for(int i=1;i<=n;i++) {
        cin>>a[i];
        //a[i]=rand()%1000000+1;
        counts[a[i]]++;
    }

    for(int i=1;i<=1000000;i++) {
        counts[i]=counts[i-1]+counts[i];
    }

    int answer = 0;
    for(int i=1;i<=n;i++) {
        int x = a[i];
        for(int j=1;j*x<=1000000;j++) {
            int L = j*x;
            int H = min(1000000, (j+1)*x-1);

            if (counts[L]==counts[H])
                continue;

            int target = counts[H];
            int low = L;
            int high = H;
            while(low < high) {
                int mid = (low + high + 1)/2;
                if(counts[mid]>=counts[H]) {
                    high = mid - 1;
                } else {
                    low = mid;
                }
            }
            answer = max(answer, (low+1) % x);

        }
    }

    cout << answer << endl;

}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int a[222222];
int counts[1111111];
int last_increase[1111111]           ;

int main() {
    int n;
    cin>>n;
    for(int i=0;i<=1000000;i++)
        counts[i]=0;
    for(int i=1;i<=n;i++) {
        cin>>a[i];
        //a[i]=rand()%1000000+1;
        counts[a[i]]++;
    }

    last_increase[0]=0;
    for(int i=1;i<=1000000;i++) {
      if (counts[i]>0) {
        last_increase[i]=i;
      } else {
        last_increase[i]=last_increase[i-1];
      }
    }

    int answer = 0;
    for(int i=1;i<=n;i++) {
        int x = a[i];
        for(int j=1;j*x<=1000000;j++) {
            int L = j*x;
            int H = min(1000000, (j+1)*x-1);

            if (last_increase[H] < L) {
              continue;
            }
            answer = max(answer, (last_increase[H]) % x);

        }
    }

    cout << answer << endl;

}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int a[222222];
int counts[1111111];
int last_increase[1111111]           ;

int main() {
    int n;
    cin>>n;
    for(int i=0;i<=1000000;i++) {
        counts[i]=0;
        done[i]=false;
    }
    for(int i=1;i<=n;i++) {
        cin>>a[i];
        //a[i]=rand()%1000000+1;
        counts[a[i]]++;
    }

    last_increase[0]=0;
    for(int i=1;i<=1000000;i++) {
      if (counts[i]>0) {
        last_increase[i]=i;
      } else {
        last_increase[i]=last_increase[i-1];
      }
    }

    int answer = 0;
    for(int i=1;i<=n;i++) {
        int x = a[i];
        if (done[x])
          continue;
        done[x]=true;
        for(int j=1;j*x<=1000000;j++) {
            int L = j*x;
            int H = min(1000000, (j+1)*x-1);

            if (last_increase[H] < L) {
              continue;
            }
            answer = max(answer, (last_increase[H]) % x);

        }
    }

    cout << answer << endl;

}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int a[222222];
int counts[1111111];
int last_increase[1111111]           ;
bool done[1111111];
int main() {
    int n;
    cin>>n;
    for(int i=0;i<=1000000;i++) {
        counts[i]=0;
        done[i]=false;
    }
    for(int i=1;i<=n;i++) {
        cin>>a[i];
        //a[i]=rand()%1000000+1;
        counts[a[i]]++;
    }

    last_increase[0]=0;
    for(int i=1;i<=1000000;i++) {
      if (counts[i]>0) {
        last_increase[i]=i;
      } else {
        last_increase[i]=last_increase[i-1];
      }
    }

    int answer = 0;
    for(int i=1;i<=n;i++) {
        int x = a[i];
        if (done[x])
          continue;
        done[x]=true;
        for(int j=1;j*x<=1000000;j++) {
            int L = j*x;
            int H = min(1000000, (j+1)*x-1);

            if (last_increase[H] < L) {
              continue;
            }
            answer = max(answer, (last_increase[H]) % x);

        }
    }

    cout << answer << endl;

}


#include <vector>
#include <stack>
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <functional>
#include <set>
#define mp(x, y) make_pair(x, y)

using namespace std;

typedef long long ll;

const ll MOD = ll(1e9) + 7;

int n;
vector <int> a;

int main(void)
{
	//freopen("key.out", "r", stdin);
	cin >> n;
	a.resize(n);
	for(int i = 0; i < n; i++)
		scanf("%d", &a[i]);

	sort(a.begin(), a.end());
	a.resize(unique(a.begin(), a.end()) - a.begin());
	n = int(a.size());
	int m = a[n - 1], best = 0;
	for(int i = 0; i < n; i++)
	{
		for(int k = 2; a[i] * (k - 1) <= m; k++)
		{
			int j = int(lower_bound(a.begin(), a.end(), a[i] * k) - a.begin() - 1);
			if(j > i)
				best = max(best, a[j] % a[i]);
		}
	}

	cout << best;

	return 0;

}


#pragma comment(linker,"/STACK:10000000000")
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;
#define FR(i,n) for(int (i)=0;(i)<(n);(i)++)
#define FOR(i,c,n) for(int (i)=(c);(i)<(n);(i)++)
#define REP(it,v,cont) for(cont::iterator (it)=(v).begin();(it)!=(v).end();++(it))
#define CLR(a,c) memset((a),(c),sizeof (a))
#define ALL(v) (v).begin(),(v).end()
#define INF 1e8
#define EPS 1e-8
#define MOD 1000000007
#define SQR(a) ((a)*(a))
typedef long long  ll;
typedef unsigned long long  ull;
typedef long double lld;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;

int main(){
    //freopen("a.in","r",stdin);
    int n;cin>>n;
    vector<int> v;
    FR(i,n){
        int k;cin>>k;
        v.push_back(k);
    }
    sort(ALL(v));
    v.erase(unique(ALL(v)),v.end());
    //FR(i,v.size()) cout<<v[i]<<endl;
    int res = 0;
    FR(i,v.size()){
        for(int j=1;v[i]*j<=v.back();j++){
            vector<int>::iterator it = lower_bound(ALL(v),(j+1)*v[i]);
            if(it!=v.begin()){
                --it;
                res=max(res,(*it-(v[i]*j)));
            }
        }
    }
    cout<<res<<endl;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;

typedef pair<int,int> PII;
typedef vector<int> VI;

const int mod=1000000007;
const int MAXN=100010;

int n;
vector<int> table;
void solve(){
	scanf("%d",&n);
	int a;
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		table.push_back(a);
	}
	sort(table.begin(),table.end());
	vector<int>::iterator it=unique(table.begin(),table.end());
	table.erase(it,table.end());
	int res=0;
	for(int i=0;i<(int)table.size()-1;i++){
		int num=2;
		while(table[i]*(num-1)<=table[n-1]){
			int tmp=lower_bound(table.begin(),table.end(),table[i]*num)-table.begin()-1;
			if(tmp>=0)
				res=max(res,table[tmp]%table[i]);
			num++;
		}
	}
	printf("%d\n",res);
}

int main()
{
	solve();
	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;

typedef pair<int,int> PII;
typedef vector<int> VI;

const int mod=1000000007;
const int MAXN=100010;

int n;
vector<int> table;
void solve(){
	scanf("%d",&n);
	int a;
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		table.push_back(a);
	}
	sort(table.begin(),table.end());
	int res=0;
	for(int i=0;i<n-1;i++){
		int num=2;
		while(table[i]*(num-1)<=table[n-1]){
			int tmp=lower_bound(table.begin(),table.end(),table[i]*num)-table.begin()-1;
			if(tmp>=0)
				res=max(res,table[tmp]%table[i]);
			num++;
		}
	}
	printf("%d\n",res);
}

int main()
{
	solve();
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
#define MN 200005
#define MM 1000005
int n, m, a[MN];
int d[MM];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		d[a[i]] = 1;
	}
	for (int i = 1; i < MM; i++) if (d[i]) a[++m] = i;
	int L, R, M, F, res = 0;
	for (int i = 1; i < m; i++) {
		for (int k = 2 * a[i]; k < a[m] + a[i]; k += a[i]) {
			L = i + 1; R = m;
			F = -1;
			while (L <= R) {
				M = (L + R) / 2;
				if (a[M] <= k) {
					L = M + 1;
					if (F < 0 || F < M) F = M;
				}
				else R = M - 1;
			}
			if (res < a[F] % a[i]) res = a[F] % a[i];
		}
	}
	printf("%d", res);
}


#include <cstdio>
#include <algorithm>
using namespace std;
#define MN 200005
#define MM 1000005
int n, m, a[MN];
int d[MM];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        d[a[i]] = 1;
    }
    for (int i = 1; i < MM; i++) if (d[i]) a[++m] = i;
    int L, R, M, F, res = 0;
    for (int i = 1; i < m; i++) {
        for (int k = 2 * a[i]; k < MM; k += a[i]) {
            L = i + 1; R = m;
            F = -1;
            while (L <= R) {
                M = (L + R) / 2;
                if (a[M] < k) {
                    L = M + 1;
                    if (F < 0 || F < M) F = M;
                }
                else R = M - 1;
            }
            if (res < a[F] % a[i]) res = a[F] % a[i];
        }
    }
    printf("%d", res);
}


#include <cstdio>
#include <algorithm>
using namespace std;
#define MN 200005
#define MM 1000005
int n, m, a[MN];
int d[MM];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        d[a[i]] = 1;
    }
    for (int i = 1; i < MM; i++) if (d[i]) a[++m] = i;
    int L, R, M, F, res = 0;
    for (int i = 1; i < m; i++) {
        for (int k = 2 * a[i]; k <= a[m] + a[i]; k += a[i]) {
            L = i + 1; R = m;
            F = -1;
            while (L <= R) {
                M = (L + R) / 2;
                if (a[M] < k) {
                    L = M + 1;
                    if (F < 0 || F < M) F = M;
                }
                else R = M - 1;
            }
            if (res < a[F] % a[i]) res = a[F] % a[i];
        }
    }
    printf("%d", res);
}


#pragma comment (linker, "/STACK:10000000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <deque>
#include <algorithm>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define pll pair <ll, ll>
#define fs first
#define sc second
#define INF (1e9 + 7)
#define INFll (1e18 + 7)
#define sort stable_sort

using namespace std;

ll n, a[1000005], b[1000005], mx, ans;

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++){
		ll t;
		cin >> t;
		mx = max(t, mx);
		a[t] = 1;
	}
	for (int i = 1; i < 1000005; i++){
		if (a[i]) b[i + 1] = i;
		else b[i + 1] = b[i];
	}
	for (int i = 0; i < 1000005; i++){
		if (a[i]){
			for (int j = 2 * i; j <= 2 * mx; j += i){
				ans = max(ans, b[j] % i);
			}
		}
	}
	cout << ans << '\n';
}


#pragma comment (linker, "/STACK:10000000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <deque>
#include <algorithm>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define pll pair <ll, ll>
#define fs first
#define sc second
#define INF (1e9 + 7)
#define INFll (1e18 + 7)
#define sort stable_sort

using namespace std;

int n, a[1000005], b[1000005], mx = 0, ans;

int main()
{
	cin >> n;
        for (int i = 0; i < n; i++){
		int t;
		cin >> t;
		mx = max(t, mx);
		a[t] = 1;
	}
	for (int i = 1; i < 1000005; i++){
		if (a[i]) b[i + 1] = i;
		else b[i + 1] = b[i];
	}
	for (int i = 0; i < 1000005; i++){
		if (a[i]){
			for (int j = 2 * i; j <= 2 * mx; j += i){
				ans = max(ans, b[j] % i);
			}
		}
	}
	cout << ans << '\n';
}


#pragma comment (linker, "/STACK:10000000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <deque>
#include <algorithm>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define pll pair <ll, ll>
#define fs first
#define sc second
#define INF (1e9 + 7)
#define INFll (1e18 + 7)
#define sort stable_sort

using namespace std;

int n, a[1000005], b[1000005], mx = 0, ans;

int main()
{
    cin >> n;
        for (int i = 0; i < n; i++){
        int t;
        cin >> t;
        mx = max(t, mx);
        a[t] = 1;
    }
    for (int i = 1; i < 1000005; i++){
        if (a[i]) b[i + 1] = i;
        else b[i + 1] = b[i];
    }
    for (int i = 0; i < 1000005; i++){
        if (a[i]){
            for (int j = 2 * i; j <= 2 * mx; j += i){
                if ( j > mx ) ans = max(ans, mx % i);
                                else ans = max(ans, b[j] % i);
            }
        }
    }
    cout << ans << '\n';
}


#pragma comment(linker,"/STACK:256000000")

#include <iostream>
#include <iomanip>

#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>

#include <algorithm>

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define sqr(x) ((x)*(x))

const int MAXVAL = 1000007;

int n;
int a[MAXVAL + 77];
int link[MAXVAL + 77];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //#define FILE_NAME "heavy"
    //freopen(FILE_NAME".in", "r", stdin);
    //freopen(FILE_NAME".out", "w", stdout);
#endif

    ios::sync_with_stdio(false);
    cin.tie(0);

    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        int x; scanf("%d", &x);
        a[x]++;
    }

    for (int i = 1; i <= MAXVAL; i++)
    {
        link[i] = link[i - 1];
        if (a[i - 1]) link[i] = i - 1;
    }

    int ans = 0;
    for (int k = MAXVAL; k >= 1; k--)
    {
        if (!a[k]) continue;

        for (int i = 2 * k; i <= MAXVAL; i += k)
        {
            int val = link[i];
            if (val < k) continue;
            ans = max(ans, val % k);
        }

        int val = link[MAXVAL];
        if (val < k) continue;
        ans = max(ans, val % k);
    }

    printf("%d", ans);

    /*
    cout << endl << 200000 << endl;
    for (int i = 1; i <= 200000; i++)
    cout << i << " ";
    */
    return 0;
}


#pragma comment(linker,"/STACK:256000000")

#include <iostream>
#include <iomanip>

#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>

#include <algorithm>

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define sqr(x) ((x)*(x))

const int MAXVAL = 1000000;

int n;
int a[1000007];
int link[1000007];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #else
    //#define FILE_NAME "heavy"
    //freopen(FILE_NAME".in", "r", stdin);
    //freopen(FILE_NAME".out", "w", stdout);
    #endif

    ios::sync_with_stdio(false);
    cin.tie(0);

    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        int x; scanf("%d", &x);
        a[x]++;
    }

    for (int i = 1; i <= MAXVAL; i++)
    {
        link[i] = link[i - 1];
        if (a[i - 1]) link[i] = i - 1;
    }

    int ans = 0;
    for (int k = MAXVAL; k >= 1; k--)
    {
        if (!a[k]) continue;

        int res = 0;
        for (int i = 2 * k; i <= MAXVAL; i += k)
        {
            int val = link[i];
            if (val < k) continue;
            res = max(res, val % k);
        }

        if (res > ans) ans = res;
    }

    printf("%d", ans);

    /*
    cout << endl << 200000 << endl;
    for (int i = 1; i <= 200000; i++)
        cout << i << " ";
    */
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <bitset>
#include <cmath>
#include <ctime>
#pragma comment(linker, "/STACK:256000000")

using namespace std;

const int maxN = 210000;

int a[maxN];
int lbound[1100000];
int n;

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }

  sort(a, a + n);
  n = unique(a, a + n) - a;

  int curidx = 0;
  for (int i = 0; i < 1100000; ++i) {
    while (curidx < n && a[curidx] < i) ++curidx;
    lbound[i] = curidx;
  }

  int res = 0;

  for (int i = n - 1; i >= 0; --i) {
    int cur = a[i];
    for (int j = 2; j <= cur; ++j) {
      int val = (cur + j - 1) / j;
      if (val == 0) break;
      if (val <= res) {
        break;
      }
      int idx = lbound[val];
      if (idx < i) {
        res = max(res, cur % a[idx]);
      }
    }
  }

  printf("%d\n", res);

  return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <bitset>
#include <cmath>
#include <ctime>
#pragma comment(linker, "/STACK:256000000")

using namespace std;

const int maxN = 210000;

int a[maxN];
int n;

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }

  sort(a, a + n);
  n = unique(a, a + n) - a;

  int res = 0;

  for (int i = n - 1; i >= 0; --i) {
    int cur = a[i];
    int mid = (cur / 2) + 1;
    int idx = lower_bound(a, a + n, mid) - a;
    if (idx < i) {
      int rem = cur % a[idx];
      res = max(res, rem);
    }
    idx = lower_bound(a, a + n, res + 1) - a;
    for (int j = idx; j < i && a[j] < mid; ++j) {
      res = max(cur % a[j], res);
    }
  }

  printf("%d\n", res);

  return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <bitset>
#include <cmath>
#include <ctime>
#pragma comment(linker, "/STACK:256000000")

using namespace std;

const int maxN = 210000;

int a[maxN];
int n;

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }

  sort(a, a + n);
  n = unique(a, a + n) - a;

  int res = 0;

  for (int i = n - 1; i >= 0; --i) {
    int cur = a[i];
    int mid = (cur / 2) + 1;
    if (cur - mid < res) break;
    int idx = lower_bound(a, a + n, mid) - a;
    if (idx < i) {
      int rem = cur % a[idx];
      res = max(res, rem);
    }
    for (int j = idx - 1; j >= 0; --j) {
      if (a[j] > res) {
        res = max(cur % a[j], res);
      } else {
        break;
      }
    }
  }

  printf("%d\n", res);

  return 0;
}


#include <vector>
#include <queue>
#include <map>
#include <list>
#include <cmath>
#include <utility>
#include <queue>
#include <algorithm>
#include <limits>
#include <set>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i=0; i<n; ++i) cin >> A[i];
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());

  if (A.size()==1)
    {
    cout << 0;
    return 0;
    }

  int best=0;
  for (int i=A.size()-1; i>=0; --i)
    {
    int Ai = A[i];
    for (int j=i-1; j>=0 && A[j]>best+1; --j)
      {
      int tmp = Ai%A[j];
      if (tmp>best) best = tmp;
      }
    }

  cout << best;

  return 0;
  }


#include <vector>
#include <queue>
#include <map>
#include <list>
#include <cmath>
#include <utility>
#include <queue>
#include <algorithm>
#include <limits>
#include <set>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  int n;
  cin >> n;
  //n=200000;
  vector<int> A(n);
  for (int i=0; i<n; ++i)
  //  A[i]=1000000-i;
  cin >> A[i];

  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());

  if (A.size()==1)
    {
    cout << 0;
    return 0;
    }

  vector<int> B(A.back()+1, -1);

  for (int i=0; i<A.size(); ++i) B[A[i]]=A[i];
  for (int i=B.size()-1; i>=0; --i) if (B[i]==-1) B[i]=B[i+1];

  int best=0;
  for (int i=A.size()-1; i>=0 && A[i]>2*best; --i)
    {
    int Ai = A[i];

    int j=2;
    for (;j<300;++j)
      {
      int k=int(Ai/j) + 1;
      int Aj = B[k];
      if (Aj<=best+1) break;
      int tmp=Ai%Aj;
      if (tmp>best) best = tmp;
      }
    if (j<300) continue;

    j = upper_bound(A.begin(), A.end(), 1+int(Ai/298))-A.begin();
    for (; j>=0 && A[j]>best+1; --j)
      {
      int tmp = Ai%A[j];
      if (tmp>best) best = tmp;
      }
    }

  cout << best;

  return 0;
  }


#include <vector>
#include <queue>
#include <map>
#include <list>
#include <cmath>
#include <utility>
#include <queue>
#include <algorithm>
#include <limits>
#include <set>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  int n;
  cin >> n;
  //n=200000;
  vector<int> A(n);
  for (int i=0; i<n; ++i)
  //  A[i]=i+1;
  cin >> A[i];

  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());

  if (A.size()==1)
    {
    cout << 0;
    return 0;
    }

  int best=0;
  for (int i=A.size()-1; i>=0 && A[i]>2*best; --i)
    {
    int Ai = A[i];
    for (int j=i-1; j>=0 && A[j]>best+1; --j)
      {
      int tmp = Ai%A[j];
      if (tmp>best) best = tmp;
      }
    }

  cout << best;

  return 0;
  }


#include <vector>
#include <queue>
#include <map>
#include <list>
#include <cmath>
#include <utility>
#include <queue>
#include <algorithm>
#include <limits>
#include <set>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  int n;
  cin >> n;
  //n=200000;
  vector<int> A(n);
  for (int i=0; i<n; ++i)
  //  A[i]=i+1;
  cin >> A[i];

  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());

  if (A.size()==1)
    {
    cout << 0;
    return 0;
    }

  int best=0;
  for (int i=A.size()-1; i>=0 && A[i]>2*best; --i)
    {
    int Ai = A[i];
    int j = upper_bound(A.begin(), A.end(), A[i]-best-1)-A.begin();
    for (; j>=0 && A[j]>best+1; --j)
      {
      int tmp = Ai%A[j];
      if (tmp>best) best = tmp;
      }
    }

  cout << best;

  return 0;
  }


#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

struct ListNode {
	     int val;
	     ListNode *next;
	     ListNode(int x) : val(x), next(NULL) {}
	 };

class Solution {
public:
	ListNode *reverse(ListNode *head) {
		ListNode *cur = head, *tail = NULL;
		while(cur) {
			ListNode *t = cur->next;
			cur->next = tail;
			tail = cur;
			cur = t;
		}
		return tail;
	}
	void reorderList(ListNode *head) {
		if(NULL == head || NULL == head->next) return;
		ListNode *p = head, *q = head, *pre;
		while(q->next != NULL) {
			pre = p;
			p = p->next;
			q = q->next->next;
		}
		pre->next = NULL;
		p = reverse(p);
		ListNode * cur = new ListNode(0);
		while(head != NULL && p != NULL) {
			cur->next = head;
			cur = head;
			head = head->next;
			cur->next = p;
			cur = p;
			p = p->next;
		}
		if(p != NULL) {
			cur->next = p;
		}
	}
};
class base
{
public:
	virtual ~base()
	{
		cout<<"base"<<endl;
	}
};
class A{
public:
	~A(){
		cout<<"a"<<endl;
	}
};
class deriverd : public base
{
public:
	A ma;
	deriverd(){
	}
	~deriverd(){
		cout<<"deriverd"<<endl;
	}
};

bool allones(long long int r, int i)
{
	for (; i>=0; i--)
	{
		if (!((r>>i)&1))
		{
			return false;
		}
	}
	return true;
}
int find(int num[], int x, int n)
{
	int low = 0, high = n, mid;
	while(low <high-1)
	{
		mid = (low+high)/2;
		if (num[mid] == x)
		{
			return num[mid];
		}
		else if (num[mid] > x)
		{
			high  = mid;
		}
		else
		{
			low = mid;
		}
	}
	return num[low];
}
int main()
{
	int n;
	cin>>n;
	int *num = new int[n];
	for (int i = 0; i<n; i++)
	{
		cin>>num[i];
	}
	sort(num, num+n);
	int ans = 0;
	for(int i = 0; i<=n-2; i++)
	{
		for (int j = 2; num[i]*j-1 <= num[n-1]; j++)
		{
			int t = find(num, num[i]*j-1, n);
			ans = max(ans, t%num[i]);
		}
	}
	cout<<ans<<endl;
}


#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

struct ListNode {
	     int val;
	     ListNode *next;
	     ListNode(int x) : val(x), next(NULL) {}
	 };

class Solution {
public:
	ListNode *reverse(ListNode *head) {
		ListNode *cur = head, *tail = NULL;
		while(cur) {
			ListNode *t = cur->next;
			cur->next = tail;
			tail = cur;
			cur = t;
		}
		return tail;
	}
	void reorderList(ListNode *head) {
		if(NULL == head || NULL == head->next) return;
		ListNode *p = head, *q = head, *pre;
		while(q->next != NULL) {
			pre = p;
			p = p->next;
			q = q->next->next;
		}
		pre->next = NULL;
		p = reverse(p);
		ListNode * cur = new ListNode(0);
		while(head != NULL && p != NULL) {
			cur->next = head;
			cur = head;
			head = head->next;
			cur->next = p;
			cur = p;
			p = p->next;
		}
		if(p != NULL) {
			cur->next = p;
		}
	}
};
class base
{
public:
	virtual ~base()
	{
		cout<<"base"<<endl;
	}
};
class A{
public:
	~A(){
		cout<<"a"<<endl;
	}
};
class deriverd : public base
{
public:
	A ma;
	deriverd(){
	}
	~deriverd(){
		cout<<"deriverd"<<endl;
	}
};

bool allones(long long int r, int i)
{
	for (; i>=0; i--)
	{
		if (!((r>>i)&1))
		{
			return false;
		}
	}
	return true;
}
int find(int num[], int x, int n)
{
	int low = 0, high = n, mid;
	while(low <high-1)
	{
		mid = (low+high)/2;
		if (num[mid] == x)
		{
			return num[mid];
		}
		else if (num[mid] > x)
		{
			high  = mid;
		}
		else
		{
			low = mid;
		}
	}
	return num[low];
}
int main()
{
	int n;
	cin>>n;
	int *num = new int[n];
	for (int i = 0; i<n; i++)
	{
		cin>>num[i];
	}
	sort(num, num+n);
	int ans = 0;
	for(int i = 0; i<=n-2; i++)
	{
		for (int j = 2; num[i]*j-1 <= num[n-1]; j++)
		{
			int t = find(num, num[i]*j-1, n);
			if (t>=num[i])
			{
				ans = max(ans, t%num[i]);
			}
		}
	}
	cout<<ans<<endl;
}


#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

struct ListNode {
	     int val;
	     ListNode *next;
	     ListNode(int x) : val(x), next(NULL) {}
	 };

class Solution {
public:
	ListNode *reverse(ListNode *head) {
		ListNode *cur = head, *tail = NULL;
		while(cur) {
			ListNode *t = cur->next;
			cur->next = tail;
			tail = cur;
			cur = t;
		}
		return tail;
	}
	void reorderList(ListNode *head) {
		if(NULL == head || NULL == head->next) return;
		ListNode *p = head, *q = head, *pre;
		while(q->next != NULL) {
			pre = p;
			p = p->next;
			q = q->next->next;
		}
		pre->next = NULL;
		p = reverse(p);
		ListNode * cur = new ListNode(0);
		while(head != NULL && p != NULL) {
			cur->next = head;
			cur = head;
			head = head->next;
			cur->next = p;
			cur = p;
			p = p->next;
		}
		if(p != NULL) {
			cur->next = p;
		}
	}
};
class base
{
public:
	virtual ~base()
	{
		cout<<"base"<<endl;
	}
};
class A{
public:
	~A(){
		cout<<"a"<<endl;
	}
};
class deriverd : public base
{
public:
	A ma;
	deriverd(){
	}
	~deriverd(){
		cout<<"deriverd"<<endl;
	}
};

bool allones(long long int r, int i)
{
	for (; i>=0; i--)
	{
		if (!((r>>i)&1))
		{
			return false;
		}
	}
	return true;
}
int find(int num[], int x, int n)
{
	int low = 0, high = n, mid;
	while(low <high-1)
	{
		mid = (low+high)/2;
		if (num[mid] == x)
		{
			return num[mid];
		}
		else if (num[mid] > x)
		{
			high  = mid;
		}
		else
		{
			low = mid;
		}
	}
	return num[low];
}
int main()
{
	int n;
	cin>>n;
	int *num = new int[n];
	for (int i = 0; i<n; i++)
	{
		cin>>num[i];
	}
	sort(num, num+n);
	int ans = 0;
	for(int i = 2; i<num[n-2]; i++)
	{
		for (int j = 2; i*j-1 <= num[n-1]; j++)
		{
			int t = find(num, i*j-1,n);
			ans = max(ans, t%i);
		}
	}
	cout<<ans<<endl;
}


#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

struct ListNode {
	     int val;
	     ListNode *next;
	     ListNode(int x) : val(x), next(NULL) {}
	 };

class Solution {
public:
	ListNode *reverse(ListNode *head) {
		ListNode *cur = head, *tail = NULL;
		while(cur) {
			ListNode *t = cur->next;
			cur->next = tail;
			tail = cur;
			cur = t;
		}
		return tail;
	}
	void reorderList(ListNode *head) {
		if(NULL == head || NULL == head->next) return;
		ListNode *p = head, *q = head, *pre;
		while(q->next != NULL) {
			pre = p;
			p = p->next;
			q = q->next->next;
		}
		pre->next = NULL;
		p = reverse(p);
		ListNode * cur = new ListNode(0);
		while(head != NULL && p != NULL) {
			cur->next = head;
			cur = head;
			head = head->next;
			cur->next = p;
			cur = p;
			p = p->next;
		}
		if(p != NULL) {
			cur->next = p;
		}
	}
};
class base
{
public:
	virtual ~base()
	{
		cout<<"base"<<endl;
	}
};
class A{
public:
	~A(){
		cout<<"a"<<endl;
	}
};
class deriverd : public base
{
public:
	A ma;
	deriverd(){
	}
	~deriverd(){
		cout<<"deriverd"<<endl;
	}
};

bool allones(long long int r, int i)
{
	for (; i>=0; i--)
	{
		if (!((r>>i)&1))
		{
			return false;
		}
	}
	return true;
}
int find(int num[], int x, int n)
{
	int low = 0, high = n, mid;
	while(low <high-1)
	{
		mid = (low+high)/2;
		if (num[mid] == x)
		{
			return num[mid];
		}
		else if (num[mid] > x)
		{
			high  = mid;
		}
		else
		{
			low = mid;
		}
	}
	return num[low];
}
const int maxn=2000000+100;
int a[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    int x;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        a[x]=x;
    }
    for(int i=0;i<maxn;i++)
    {
        if(a[i]!=i)
        a[i]=a[i-1];
    }
    int ans=0;
    for(int i=2;i<maxn;i++)
    {
        if(a[i]==i)
        {
            for(int j=i+i-1;j<maxn;j=j+i)
            {
                if(a[j]%i>ans&&a[j]>i)
                ans=a[j]%i;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

struct ListNode {
	     int val;
	     ListNode *next;
	     ListNode(int x) : val(x), next(NULL) {}
	 };

class Solution {
public:
	ListNode *reverse(ListNode *head) {
		ListNode *cur = head, *tail = NULL;
		while(cur) {
			ListNode *t = cur->next;
			cur->next = tail;
			tail = cur;
			cur = t;
		}
		return tail;
	}
	void reorderList(ListNode *head) {
		if(NULL == head || NULL == head->next) return;
		ListNode *p = head, *q = head, *pre;
		while(q->next != NULL) {
			pre = p;
			p = p->next;
			q = q->next->next;
		}
		pre->next = NULL;
		p = reverse(p);
		ListNode * cur = new ListNode(0);
		while(head != NULL && p != NULL) {
			cur->next = head;
			cur = head;
			head = head->next;
			cur->next = p;
			cur = p;
			p = p->next;
		}
		if(p != NULL) {
			cur->next = p;
		}
	}
};
class base
{
public:
	virtual ~base()
	{
		cout<<"base"<<endl;
	}
};
class A{
public:
	~A(){
		cout<<"a"<<endl;
	}
};
class deriverd : public base
{
public:
	A ma;
	deriverd(){
	}
	~deriverd(){
		cout<<"deriverd"<<endl;
	}
};

bool allones(long long int r, int i)
{
	for (; i>=0; i--)
	{
		if (!((r>>i)&1))
		{
			return false;
		}
	}
	return true;
}
int find(int num[], int x, int n)
{
	int low = 0, high = n, mid;
	while(low <high-1)
	{
		mid = (low+high)/2;
		if (num[mid] == x)
		{
			return num[mid];
		}
		else if (num[mid] > x)
		{
			high  = mid;
		}
		else
		{
			low = mid;
		}
	}
	return num[low];
}
int main()
{
	int n;
	cin>>n;
	int *num = new int[n];
	for (int i = 0; i<n; i++)
	{
		cin>>num[i];
	}
	sort(num, num+n);
	int ans = 0;
	for(int i = 0; i<=n-2; i++)
	{
		for (int j = 2; num[i]*(j-1)-1 <= num[n-1]; j++)
		{
			int t = find(num, num[i]*j-1, n);
			if (t>=num[i])
			{
				ans = max(ans, t%num[i]);
			}
		}
	}
	cout<<ans<<endl;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <map>
#include <climits>
#include <cstdio>
#include <queue>
#include <stack>
#include <bitset>
#include <list>
#include <iomanip>
#include <memory.h>
#include <ctime>
#include <cstring>
#include <windows.h>
#include <tchar.h>

using namespace std;

typedef long long int ll;
typedef long double de;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vc> vvc;
typedef vector<vl> vvl;
typedef vector<vb> vvb;
typedef vector<vs> vvs;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;

#define all(x) x.begin(),x.end()
#define maxlim(x) numeric_limits<x>::max()
#define matrix_out(x) for(int i = 0;i < x.size();i++) { for(int j = 0;j < x[i].size();j++) { cout << x[i][j] << ' ';} cout << endl;}

const int MAXN = 2*1000001;

int f[MAXN];

int main()
{
	ios_base::sync_with_stdio(false);
	int n,t,ans = 0;
	cin >> t;
	for(int i = 0; i < n;i++)
	{
		cin >> t;
		f[t] = t;
	}
	for(int i = 1;i < MAXN;i++)
		if(!f[i]) f[i] = f[i-1];
	for(int i = 1;i < MAXN;i++)
		if(f[i] == i)
			for(int j = 2*i-1;j < MAXN;j+=i)
				ans = max(ans,f[j]%i);
	cout << ans << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<algorithm>
#include<vector>

using namespace std;

typedef long long ll;

const int MX = 1000000;
int N;
bool chk[2000006];
int num[2000006];
//*
int main()
{
    scanf("%d", &N);
    for(int i=1;i<=N;i++){
        int A;
        scanf("%d", &A);
        chk[A] = true;
    }
    for(int i=1;i<=2*MX;i++){
        if( chk[i-1] ) num[i] = 1;
        else num[i] = num[i-1]+1;
    }
    int ans = 0;
    for(int i=2;i<=MX;i++){
        if( !chk[i] ) continue;
        for(int j=2*i;j<=2*MX;j+=i){
            if( num[j] < i ) ans = max(ans, i - num[j]);
        }
    }
    printf("%d", ans);
    return 0;
}
//*/
/*
int main()
{
    freopen("input.txt","w",stdout);
    printf("200000\n");
    for(int i=1;i<=200000;i++){
        printf("%d ", i);
    }
}
//*/


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<algorithm>
#include<vector>

using namespace std;

typedef long long ll;

const int MX = 1000000;
int N;
bool chk[1000006];
int num[1000006];
//*
int main()
{
    scanf("%d", &N);
    for(int i=1;i<=N;i++){
        int A;
        scanf("%d", &A);
        chk[A] = true;
    }
    for(int i=1;i<=MX;i++){
        if( chk[i-1] ) num[i] = 1;
        else num[i] = num[i-1]+1;
    }
    int ans = 0;
    for(int i=2;i<=MX;i++){
        if( !chk[i] ) continue;
        for(int j=2*i;j<=MX;j+=i){
            if( num[j] < i ) ans = max(ans, i-num[j]);
        }
    }
    printf("%d", ans);
    return 0;
}
//*/
/*
int main()
{
    freopen("input.txt","w",stdout);
    printf("200000\n");
    for(int i=1;i<=200000;i++){
        printf("%d ", i);
    }
}
//*/


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<algorithm>
#include<vector>

using namespace std;

typedef long long ll;

const int MX = 1000000;
int N;
bool chk[1000006];
int num[1000006];
//*
int main()
{
    scanf("%d", &N);
    for(int i=1;i<=N;i++){
        int A;
        scanf("%d", &A);
        chk[A] = true;
    }
    for(int i=1;i<=MX;i++){
        if( chk[i-1] ) num[i] = 1;
        else num[i] = num[i-1]+1;
    }
    int ans = 0;
    for(int i=2;i<=MX;i++){
        if( !chk[i] ) continue;
        for(int j=i;j<=MX;j+=i){
            if( num[j] ) ans = max(ans, i-num[j]);
        }
    }
    printf("%d", ans);
    return 0;
}
//*/
/*
int main()
{
    freopen("input.txt","w",stdout);
    printf("200000\n");
    for(int i=1;i<=200000;i++){
        printf("%d ", i);
    }
}
//*/


#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

int a[200007];
int f[2000007];
int ans[200007];
int max(int a, int b){
	return (a > b) ? a : b;
}
int main()
{
	int n;
	scanf_s("%d", &n);
	/*int r = 0;
	for (int i = 1; i <= 1000000; i++) r += 2000000 / i;
	cout << r;
	cin >> r;*/
	for (int i = 0; i < n; i++)
		scanf_s("%d", &a[i]);
	sort(a, a + n);
	for (int i = 0; i < n - 1; i++)
	for (int j = a[i] + 1; j <= a[i + 1]; j++)
	{
		f[j] = a[i];
	}
	int w = 2 * a[n - 1] + 3;
	for (int j = a[n - 1] + 1; j <= w; j++)
		f[j] = a[n - 1];
	for (int i = 0; i < n; i++)
	for (int j = 2*a[i]; j <=w; j+=a[i])
	{
		if(i==0||(i>0&&a[i]>a[i-1])) ans[i] = max(ans[i], f[j] % a[i]);
	}
	int mx = 0;
	for (int i = 0; i < n; i++)
		mx = max(mx, ans[i]);
	printf("%d", mx);
	//cin >> n;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

int a[200007];
int f[2000007];
int ans[200007];

int main()
{
	int n;
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
		scanf_s("%d", &a[i]);
	sort(a, a + n);
	for (int i = 0; i < n - 1; i++)
	for (int j = a[i] + 1; j <= a[i + 1]; j++)
	{
		f[j] = a[i];
	}
	for (int j = a[n - 1] + 1; j <= 2 * a[n - 1]; j++)
		f[j] = a[n - 1];
	for (int i = 0; i < n; i++)
	for (int j = 2; j*a[i] <= 2000003; j++)
	{
		ans[i] = max(ans[i], f[j*a[i]] % a[i]);
	}
	int mx = 0;
	for (int i = 0; i < n; i++)
		mx = max(mx, ans[i]);
	printf("%d", mx);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

int a[200007];
int f[2000007];
int ans[200007];
int max(int a, int b){
	return (a > b) ? a : b;
}
int main()
{
	int n;
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
		scanf_s("%d", &a[i]);
	sort(a, a + n);
	for (int i = 0; i < n - 1; i++)
	for (int j = a[i] + 1; j <= a[i + 1]; j++)
	{
		f[j] = a[i];
	}
	for (int j = a[n - 1] + 1; j <= 2 * a[n - 1]; j++)
		f[j] = a[n - 1];
	for (int i = 0; i < n; i++)
	for (int j = 2; j*a[i] <= 2*a[n-1]+3; j++)
	{
		ans[i] = max(ans[i], f[j*a[i]] % a[i]);
	}
	int mx = 0;
	for (int i = 0; i < n; i++)
		mx = max(mx, ans[i]);
	printf("%d", mx);
	//cin >> n;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

int a[200007];
int f[2000007];
int ans[200007];
int max(int a, int b){
	return (a > b) ? a : b;
}
int main()
{
	int n;
	scanf_s("%d", &n);
	/*int r = 0;
	for (int i = 1; i <= 1000000; i++) r += 2000000 / i;
	cout << r;
	cin >> r;*/
	for (int i = 0; i < n; i++)
		scanf_s("%d", &a[i]);
	sort(a, a + n);
	for (int i = 0; i < n - 1; i++)
	for (int j = a[i] + 1; j <= a[i + 1]; j++)
	{
		f[j] = a[i];
	}
	int w = 2 * a[n - 1] + 3;
	for (int j = a[n - 1] + 1; j <= w; j++)
		f[j] = a[n - 1];
	for (int i = 0; i < n; i++)
	if (i == 0 || (i>0 && a[i]>a[i - 1])){
		for (int j = 2 * a[i]; j <= w; j += a[i])
		{
			ans[i] = max(ans[i], f[j] % a[i]);
		}
	}
	int mx = 0;
	for (int i = 0; i < n; i++)
		mx = max(mx, ans[i]);
	printf("%d", mx);
	//cin >> n;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

int a[200007];
int f[2000007];
int ans[200007];
int max(int a, int b){
	return (a > b) ? a : b;
}
int main()
{
	int n;
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
		scanf_s("%d", &a[i]);
	sort(a, a + n);
	for (int i = 0; i < n - 1; i++)
	for (int j = a[i] + 1; j <= a[i + 1]; j++)
	{
		f[j] = a[i];
	}
	for (int j = a[n - 1] + 1; j <= 2 * a[n - 1]; j++)
		f[j] = a[n - 1];
	for (int i = 0; i < n; i++)
	for (int j = 2*a[i]; j <= 2*a[n-1]+3; j+=a[i])
	{
		ans[i] = max(ans[i], f[j] % a[i]);
	}
	int mx = 0;
	for (int i = 0; i < n; i++)
		mx = max(mx, ans[i]);
	printf("%d", mx);
	//cin >> n;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

int a[200007];
int f[2000007];
int ans[200007];
int max(int a, int b){
	return (a > b) ? a : b;
}
int main()
{
	int n;
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
		scanf_s("%d", &a[i]);
	sort(a, a + n);
	for (int i = 0; i < n - 1; i++)
	for (int j = a[i] + 1; j <= a[i + 1]; j++)
	{
		f[j] = a[i];
	}
	for (int j = a[n - 1] + 1; j <= 2 * a[n - 1]; j++)
		f[j] = a[n - 1];
	for (int i = 0; i < n; i++)
	for (int j = 2*a[i]; j <= 2*a[n-1]+3; j+=a[i])
	{
		ans[i] = max(ans[i], f[j*a[i]] % a[i]);
	}
	int mx = 0;
	for (int i = 0; i < n; i++)
		mx = max(mx, ans[i]);
	printf("%d", mx);
	//cin >> n;
	return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n,v[200000],prv[1000001],nxt[1000001];
bool e[1000001];
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%d",v+i);
        e[v[i]]=true;
    }
    int last=1;
    for(int i=1;i<=1000000;++i){
        prv[i]=last;
        if(e[i])
            last=i;
    }
    last=1;
    for(int i=1000000;i>0;--i){
        nxt[i]=last;
        if(e[i])
            last=i;
    }
    sort(v,v+n);
    n=unique(v,v+n)-v;
    int res=0,val;
    for(int i=0;i<n;++i){
        val=v[i];
        for(int j=val;j<=1000000;j+=val){
            res=max(res,nxt[j]%val);
            if(prv[j]>val)
                res=max(res,prv[j]%val);
        }
    }
    printf("%d\n",res);
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n,v[200000],prv[1000001],nxt[1000001];
bool e[1000001];
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%d",v+i);
        e[v[i]]=true;
    }
    int last=1;
    for(int i=1;i<=1000000;++i){
        prv[i]=last;
        if(e[i])
            last=i;
    }
    last=1;
    for(int i=1000000;i>0;--i){
        nxt[i]=last;
        if(e[i])
            last=i;
    }
    sort(v,v+n);
    n=unique(v,v+n)-v;
    int res=0,val;
    for(int i=0;i<n;++i){
        val=v[i];
        for(int j=val;j<=1000000;j+=val){
            if(nxt[j]>val)
                res=max(res,nxt[j]%val);
            if(prv[j]>val)
                res=max(res,prv[j]%val);
        }
    }
    printf("%d\n",res);
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n,v[200000],prv[2000001],nxt[2000001];
bool e[2000001];
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%d",v+i);
        e[v[i]]=true;
    }
    int last=1;
    for(int i=1;i<=2000000;++i){
        prv[i]=last;
        if(e[i])
            last=i;
    }
    last=1;
    for(int i=2000000;i>0;--i){
        nxt[i]=last;
        if(e[i])
            last=i;
    }
    sort(v,v+n);
    n=unique(v,v+n)-v;
    int res=0,val;
    for(int i=0;i<n;++i){
        val=v[i];
        for(int j=val;j<=2000000;j+=val){
            if(nxt[j]>val)
                res=max(res,nxt[j]%val);
            if(prv[j]>val)
                res=max(res,prv[j]%val);
        }
    }
    printf("%d\n",res);
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n,v[200000],prv[1000001];
bool e[1000001];
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%d",v+i);
        e[v[i]]=true;
    }
    int last=1;
    for(int i=1;i<=1000000;++i){
        prv[i]=last;
        if(e[i])
            last=i;
    }
    sort(v,v+n);
    n=unique(v,v+n)-v;
    int res=0,val;
    for(int i=0;i<n;++i){
        val=v[i];
        res=max(res,v[n-1]%val);
        for(int j=val;j<=1000000;j+=val)
            if(prv[j]>val)
                res=max(res,prv[j]%val);
    }
    printf("%d\n",res);
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
const int MAX = 2000001;
int a[MAX];
int b[MAX];

int findfor(int k){

    int rest = 0;

    for(int i = 2*k; i <= MAX; i+=k){
        rest = max(rest, b[i] % k);
    }

    return rest;

}

int main(){

    int n;
    cin >> n;

    for(int i = 0; i < n; i++){
        int x;
        scanf("%d",&x);
        a[x] = 1;
    }

    for(int i = 1; i < MAX; i++){
        if(a[i]) b[i+1] = i;
        if(!b[i]) b[i] = b[i-1];
    }

    int rest = 0;

    for(int i = 0; i < MAX; i++){
        if(a[i]){
            rest = max(rest,findfor(i));
        }
    }

    cout << rest;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
const int MAX = 2000001;
int mx = 0;

vector<int> a(MAX);

void updatea(){
    int num = 0;

    for(int i = 0; i <= mx; i++){
        bool fm = false;
        if(a[i]) fm = true;
        a[i] = num;
        if(fm) num = i;
    }
}

int findfor(int k){

    int rest = 0;

    for(int i = 2; i * k <= mx; i++){
        rest = max(rest, a[i*k] % k);
    }

    return rest;

}
int main(){

    int n;
    cin >> n;

    vector<int> v(n);

    for(int i = 0; i < n; i++){
        cin >> v[i];
        a[v[i]] = 1;
        mx = max(mx,v[i]);
    }

    mx = mx << 1;

    sort(v.begin(),v.end());

    updatea();

    int rest = 0;

    for(int i = 0; i < v.size(); i++){
        rest = max(rest,findfor(v[i]));
    }

    cout << rest;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
const int MAX = 2000001;
int mx = 0;

vector<int> a(MAX);

void updatea(){
    int num = 0;

    for(int i = 0; i <= mx; i++){
        bool fm = false;
        if(a[i]) fm = true;
        a[i] = num;
        if(fm) num = i;
    }
}

int findfor(int k){

    int rest = 0;

    for(int i = 2; i * k <= mx; i++){
        int tmp = a[i*k];
        while(tmp >= k) tmp -=k;
        rest = max(rest, tmp);
    }

    return rest;

}
int main(){

    int n;
    cin >> n;

    vector<int> v(n);

    for(int i = 0; i < n; i++){
        cin >> v[i];
        a[v[i]] = 1;
        mx = max(mx,v[i]);
    }

    mx = mx << 1;

    sort(v.begin(),v.end());

    updatea();

    int rest = 0;

    for(int i = 0; i < v.size(); i++){
        rest = max(rest,findfor(v[i]));
    }

    cout << rest;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
const int MAX = 2000001;
int a[MAX];
int b[MAX];

int main(){

    int n;
    cin >> n;

    for(int i = 0; i < n; i++){
        int x;
        scanf("%d",&x);
        a[x] = 1;
    }

    for(int i = 1; i < MAX; i++){
        if(a[i]) b[i+1] = i;
        if(!b[i]) b[i] = b[i-1];
    }

    int rest = 0;

    for(int i = 0; i < MAX; i++){
        if(a[i]){
            for(int j = i + i; j < MAX; j+=i){
                rest = max(rest,b[j] % i);
            }
        }
    }

    cout << rest;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
const int MAX = 2000001;
int mx = 0;

vector<int> a(MAX);

void updatea(){
    int num = 0;

    for(int i = 0; i <= mx; i++){
        bool fm = false;
        if(a[i]) fm = true;
        a[i] = num;
        if(fm) num = i;
    }
}

int findfor(int k){

    int rest = 0;

    for(int i = 2*k; i <= mx; i+=k){
        rest = max(rest, a[i] % k);
    }

    return rest;

}
int main(){

    int n;
    cin >> n;

    vector<int> v(n);

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

    mx = mx << 1;

    updatea();

    int rest = 0;

    for(int i = 0; i < v.size(); i++){
        rest = max(rest,findfor(v[i]));
    }

    cout << rest;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
const int MAX = 2000001;
int a[MAX];
int b[MAX];

int findfor(int k){

    int rest = 0;

    for(int i = 2*k; i <= MAX; i+=k){
        rest = max(rest, b[i] % k);
    }

    return rest;

}

int main(){

    int n;
    cin >> n;

    vector<int> v(n);

    for(int i = 0; i < n; i++){
        scanf("%d",&v[i]);
    }

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

    for(int i = 1; i < MAX; i++){
        if(a[i]) b[i+1] = i;
        if(!b[i]) b[i] = b[i-1];
    }

    sort(v.begin(),v.end());

    int rest = 0;

    for(int i = 0; i < v.size(); i++){
        rest = max(rest,findfor(v[i]));
    }

    cout << rest;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
const int MAX = 2000001;
int mx = 0;

vector<int> a(MAX);

void updatea(){
    int num = 0;

    for(int i = 0; i <= mx; i++){
        bool fm = false;
        if(a[i]) fm = true;
        a[i] = num;
        if(fm) num = i;
    }
}

int findfor(int k){

    int rest = 0;

    for(int i = 2; i * k <= mx; i++){
        rest = max(rest, a[i*k] % k);
    }

    return rest;

}
int main(){

    int n;
    cin >> n;

    vector<int> v(n);

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

    mx = mx << 1;

    updatea();

    int rest = 0;

    for(int i = 0; i < v.size(); i++){
        rest = max(rest,findfor(v[i]));
    }

    cout << rest;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
const int MAX = 2000001;
int a[MAX];
int b[MAX];

int findfor(int k){

    int rest = 0;

    for(int i = 2*k; i <= MAX; i+=k){
        rest = max(rest, b[i] % k);
    }

    return rest;

}

int main(){

    int n;
    cin >> n;

    vector<int> v(n);

    for(int i = 0; i < n; i++){
        scanf("%d",&v[i]);
    }

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

    for(int i = 1; i < MAX; i++){
        if(a[i]) b[i+1] = i;
        if(!b[i]) b[i] = b[i-1];
    }

    int rest = 0;

    for(int i = 0; i < v.size(); i++){
        rest = max(rest,findfor(v[i]));
    }

    cout << rest;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

int n;
int ans;
int a[200100];
int b[1000100];
int INF = (int)1e6 + 1;
int MAGIC = 1000;

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    sort(a, a + n);
    n = unique(a, a + n) - a;
    a[n] = INF;
    int it = 1;
    int x = -1;
    for (int i = 0; i <= n; i++)
    {
        while (it < a[i])
        {
            b[it++] = x;
        }
        x = a[i];
    }
    for (int i = 0; i < n; i++)
    {
        if (a[i] < MAGIC)
        {
            for (int j = i + 1; j < n; j++)
                ans = max(ans, a[j] % a[i]);
        }
        else
        {
            for (int k = 2; a[i] * k < INF; k++)
            {
                int x = b[ a[i] * k - 1 ];
                ans = max(ans, x - a[i] * (k - 1));
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

int n;
int ans;
int a[200100];
int b[1000100];
int INF = (int)1e6 + 1;
int MAGIC = 1000;

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    sort(a, a + n);
    n = unique(a, a + n) - a;
    a[n] = INF;
    int it = 1;
    int x = -1;
    for (int i = 0; i <= n; i++)
    {
        while (it < a[i])
        {
            b[it++] = x;
        }
        x = a[i];
    }
    b[INF] = a[n - 1];
    for (int i = 0; i < n; i++)
    {
        if (a[i] < MAGIC)
        {
            for (int j = i + 1; j < n; j++)
                ans = max(ans, a[j] % a[i]);
        }
        else
        {
            for (int k = 2; a[i] * (k - 1) < INF; k++)
            {
                int x = b[ min(a[i] * k - 1, INF) ];
                ans = max(ans, x - a[i] * (k - 1));
            }
        }
    }
    printf("%d\n", ans);

    return 0;
}


#pragma comment(linker, "/STACK:336777216")
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>

typedef long long ll;

using namespace std;
inline ll MAX(ll a, ll b) { return (a > b) ? (a) : (b); }
inline ll MIN(ll a, ll b) { return (a < b) ? (a) : (b); }

#define rep(i,a,b) for(i=a; i<=b; i++)
#define brep(i,b,a) for(i=b; i>=a; i--)
#define sd second
#define ft first
//#define mp make_pair

ll f(ll x, ll n)
{
	if (x < 0) return 0;
	if (x%n == 0) return x / n;
	return x / n + 1;
}

vector<ll>v;
ll l;

int find(ll val)
{
	int l2 = 0, r = l,m;

	while (r - l2 > 1)
	{
		m = (l2 + r) / 2;
		if (v[m] >= val) r = m;
		else l2 = m;
	}

	return l2;
}
int main()
{
	int const sz = 2e5 + 10;

	ll  n,m,a[sz+1],b,k,i,sum=0, j, x, y,r,q,z,dp[sz+1],t,lab[sz+1],ans=0,pr[sz+1];

	cin >> n;

	rep(i, 1, n) cin >> a[i];

	sort(a + 1, a + n + 1);

	a[0] = -1;
	rep(i, 1, n) if (a[i] != a[i - 1]) v.push_back(a[i]);

	l = v.size();

	v.push_back(1e18);
	ll max_rest = 0;
	rep(i, 0, l - 1)
	{
		for(j= 2*v[i];j<= 2 * v[l - 1];j+=v[i])
		{
			ll rest = find(j);
			if (rest <= i) continue;
			rest = v[rest]-j+v[i];

			if (rest<v[i] && rest>=0 && rest > max_rest) max_rest = rest;
		}
	}

	cout << max_rest;

	return 0;
}


#pragma comment(linker, "/STACK:336777216")
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>

typedef long long ll;

using namespace std;
inline ll MAX(ll a, ll b) { return (a > b) ? (a) : (b); }
inline ll MIN(ll a, ll b) { return (a < b) ? (a) : (b); }

#define rep(i,a,b) for(i=a; i<=b; i++)
#define brep(i,b,a) for(i=b; i>=a; i--)
#define sd second
#define ft first
//#define mp make_pair

ll f(ll x, ll n)
{
	if (x < 0) return 0;
	if (x%n == 0) return x / n;
	return x / n + 1;
}

vector<ll>v;
ll l;

int find(ll val)
{
	int l2 = 0, r = l,m;

	while (r - l2 > 1)
	{
		m = (l2 + r) / 2;
		if (v[m] >= val) r = m;
		else l2 = m;
	}

	return l2;
}
int main()
{
	int const sz = 2e5 + 10;

	ll  n,m,a[sz+1],b,k,i,sum=0, j, x, y,r,q,z,dp[sz+1],t,lab[sz+1],ans=0,pr[sz+1];

	cin >> n;

	rep(i, 1, n) cin >> a[i];

	sort(a + 1, a + n + 1);

	a[0] = -1;
	rep(i, 1, n) if (a[i] != a[i - 1]) v.push_back(a[i]);

	l = v.size();

	v.push_back(1e18);
	ll max_rest = 0;
	rep(i, 0, l - 1)
	{
		for(j= 2*v[i];j<= v[l - 1]+v[i];j+=v[i])
		{
			ll rest = find(j);
			if (rest <= i) continue;
			rest = v[rest]-j+v[i];

			if (rest<v[i] && rest>=0 && rest > max_rest) max_rest = rest;
		}
	}

	cout << max_rest;

	return 0;
}


#pragma comment(linker, "/STACK:336777216")
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>

typedef long long ll;

using namespace std;
inline ll MAX(ll a, ll b) { return (a > b) ? (a) : (b); }
inline ll MIN(ll a, ll b) { return (a < b) ? (a) : (b); }

#define rep(i,a,b) for(i=a; i<=b; i++)
#define brep(i,b,a) for(i=b; i>=a; i--)
#define sd second
#define ft first
//#define mp make_pair

ll f(ll x, ll n)
{
	if (x < 0) return 0;
	if (x%n == 0) return x / n;
	return x / n + 1;
}

vector<ll>v;
ll l;

ll find(ll val)
{
	int l2 = 0, r = l,m;

	while (r - l2 > 1)
	{
		m = (l2 + r) / 2;
		if (v[m] >= val) r = m;
		else l2 = m;
	}

	return l2;
}
int main()
{
	int const sz = 2e3 + 10;

	ll  n,m,a[sz+1],b,k,i,sum=0, j, x, y,r,q,z,dp[sz+1],t,lab[sz+1],ans=0,pr[sz+1];

	cin >> n;

	rep(i, 1, n) cin >> a[i];

	sort(a + 1, a + n + 1);

	a[0] = -1;
	rep(i, 1, n) if (a[i] != a[i - 1]) v.push_back(a[i]);

	l = v.size();

	v.push_back(1e18);
	ll max_rest = 0;
	rep(i, 0, l - 1)
	{
		rep(j, 2, 2 * v[l - 1] / v[i])
		{
			ll rest = find(j*v[i]);
			if (rest <= i) continue;
			rest = v[rest]%v[i];

			if (rest > max_rest) max_rest = rest;
		}
	}

	cout << max_rest;

	return 0;
}


#pragma comment(linker, "/STACK:336777216")
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>

typedef long long ll;

using namespace std;
inline ll MAX(ll a, ll b) { return (a > b) ? (a) : (b); }
inline ll MIN(ll a, ll b) { return (a < b) ? (a) : (b); }

#define rep(i,a,b) for(i=a; i<=b; i++)
#define brep(i,b,a) for(i=b; i>=a; i--)
#define sd second
#define ft first
//#define mp make_pair

ll f(ll x, ll n)
{
	if (x < 0) return 0;
	if (x%n == 0) return x / n;
	return x / n + 1;
}

vector<ll>v;
ll l;

ll find(ll val)
{
	int l2 = 0, r = l,m;

	while (r - l2 > 1)
	{
		m = (l2 + r) / 2;
		if (v[m] >= val) r = m;
		else l2 = m;
	}

	return l2;
}
int main()
{
	int const sz = 2e5 + 10;

	ll  n,m,a[sz+1],b,k,i,sum=0, j, x, y,r,q,z,dp[sz+1],t,lab[sz+1],ans=0,pr[sz+1];

	cin >> n;

	rep(i, 1, n) cin >> a[i];

	sort(a + 1, a + n + 1);

	a[0] = -1;
	rep(i, 1, n) if (a[i] != a[i - 1]) v.push_back(a[i]);

	l = v.size();

	v.push_back(1e18);
	ll max_rest = -1;
	rep(i, 0, l - 1)
	{
		rep(j, 2, 2 * v[l - 1] / v[i])
		{
			ll rest = find(j*v[i]);
			rest = j*v[i]-a[rest];
			if (rest >= v[i]) continue;
			if (rest > max_rest) max_rest = rest;
		}
	}

	cout << max_rest;

	return 0;
}


#pragma comment(linker, "/STACK:336777216")
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>

typedef long long ll;

using namespace std;
inline ll MAX(ll a, ll b) { return (a > b) ? (a) : (b); }
inline ll MIN(ll a, ll b) { return (a < b) ? (a) : (b); }

#define rep(i,a,b) for(i=a; i<=b; i++)
#define brep(i,b,a) for(i=b; i>=a; i--)
#define sd second
#define ft first
//#define mp make_pair

ll f(ll x, ll n)
{
	if (x < 0) return 0;
	if (x%n == 0) return x / n;
	return x / n + 1;
}

vector<ll>v;
ll l;

ll find(ll val)
{
	int l2 = 0, r = l,m;

	while (r - l2 > 1)
	{
		m = (l2 + r) / 2;
		if (v[m] >= val) r = m;
		else l2 = m;
	}

	return l2;
}
int main()
{
	int const sz = 2e5 + 10;

	ll  n,m,a[sz+1],b,k,i,sum=0, j, x, y,r,q,z,dp[sz+1],t,lab[sz+1],ans=0,pr[sz+1];

	cin >> n;

	rep(i, 1, n) cin >> a[i];

	sort(a + 1, a + n + 1);

	a[0] = -1;
	rep(i, 1, n) if (a[i] != a[i - 1]) v.push_back(a[i]);

	l = v.size();

	v.push_back(1e18);
	ll max_rest = 0;
	rep(i, 0, l - 1)
	{
		rep(j, 2, 2 * v[l - 1] / v[i])
		{
			ll rest = find(j*v[i]);
			if (rest <= i) continue;
			rest = v[rest]%v[i];

			if (rest > max_rest) max_rest = rest;
		}
	}

	cout << max_rest;

	return 0;
}


#pragma comment(linker, "/STACK:336777216")
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>

typedef long long ll;

using namespace std;
inline ll MAX(ll a, ll b) { return (a > b) ? (a) : (b); }
inline ll MIN(ll a, ll b) { return (a < b) ? (a) : (b); }

#define rep(i,a,b) for(i=a; i<=b; i++)
#define brep(i,b,a) for(i=b; i>=a; i--)
#define sd second
#define ft first
//#define mp make_pair

ll f(ll x, ll n)
{
	if (x < 0) return 0;
	if (x%n == 0) return x / n;
	return x / n + 1;
}

vector<int>v;
ll l;

ll find(ll val)
{
	int l2 = 0, r = l,m;

	while (r - l2 > 1)
	{
		m = (l2 + r) / 2;
		if (v[m] >= val) r = m;
		else l2 = m;
	}

	return l2;
}
int main()
{
	int const sz = 2e5 + 10;

	ll  n,m,a[sz+1],b,k,i,sum=0, j, x, y,r,q,z,dp[sz+1],t,lab[sz+1],ans=0,pr[sz+1];

	cin >> n;

	rep(i, 1, n) cin >> a[i];

	sort(a + 1, a + n + 1);

	a[0] = -1;
	rep(i, 1, n) if (a[i] != a[i - 1]) v.push_back(a[i]);

	l = v.size();

	v.push_back(1e18);
	ll max_rest = 0;
	rep(i, 0, l - 1)
	{
		rep(j, 2, 2 * v[l - 1] / v[i])
		{
			ll rest = find(j*v[i]);
			if (rest <= i) continue;
			rest = v[rest]%v[i];

			if (rest > max_rest) max_rest = rest;
		}
	}

	cout << max_rest;

	return 0;
}


#pragma comment(linker, "/STACK:336777216")
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>

typedef long long ll;

using namespace std;
inline ll MAX(ll a, ll b) { return (a > b) ? (a) : (b); }
inline ll MIN(ll a, ll b) { return (a < b) ? (a) : (b); }

#define rep(i,a,b) for(i=a; i<=b; i++)
#define brep(i,b,a) for(i=b; i>=a; i--)
#define sd second
#define ft first
//#define mp make_pair

ll f(ll x, ll n)
{
	if (x < 0) return 0;
	if (x%n == 0) return x / n;
	return x / n + 1;
}

vector<ll>v;
ll l;

ll find(ll val)
{
	int l2 = 0, r = l,m;

	while (r - l2 > 1)
	{
		m = (l2 + r) / 2;
		if (v[m] >= val) r = m;
		else l2 = m;
	}

	return l2;
}
int main()
{
	int const sz = 2e5 + 10;

	ll  n,m,a[sz+1],b,k,i,sum=0, j, x, y,r,q,z,dp[sz+1],t,lab[sz+1],ans=0,pr[sz+1];

	cin >> n;

	rep(i, 1, n) cin >> a[i];

	sort(a + 1, a + n + 1);

	a[0] = -1;
	rep(i, 1, n) if (a[i] != a[i - 1]) v.push_back(a[i]);

	l = v.size();

	v.push_back(1e18);
	ll max_rest = 0;
	rep(i, 0, l - 1)
	{
		rep(j, 2, 2 * v[l - 1] / v[i])
		{
			ll rest = find(j*v[i]);
			if (rest <= i) continue;
			rest = v[rest]-v[i]*(j-1);

			if (rest<v[i] && rest>=0 && rest > max_rest) max_rest = rest;
		}
	}

	cout << max_rest;

	return 0;
}


#pragma comment(linker, "/STACK:336777216")
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>

typedef long long ll;

using namespace std;
inline ll MAX(ll a, ll b) { return (a > b) ? (a) : (b); }
inline ll MIN(ll a, ll b) { return (a < b) ? (a) : (b); }

#define rep(i,a,b) for(i=a; i<=b; i++)
#define brep(i,b,a) for(i=b; i>=a; i--)
#define sd second
#define ft first
//#define mp make_pair

ll f(ll x, ll n)
{
	if (x < 0) return 0;
	if (x%n == 0) return x / n;
	return x / n + 1;
}

vector<ll>v;
ll l;

ll find(ll val)
{
	int l2 = 0, r = l,m;

	while (r - l2 > 1)
	{
		m = (l2 + r) / 2;
		if (v[m] >= val) r = m;
		else l2 = m;
	}

	return l2;
}
int main()
{
	int const sz = 2e3 + 10;

	ll  n,m,a[sz+1],b,k,i,sum=0, j, x, y,r,q,z,dp[sz+1],t,lab[sz+1],ans=0,pr[sz+1];

	cin >> n;

	rep(i, 1, n) cin >> a[i];

	sort(a + 1, a + n + 1);

	a[0] = -1;
	rep(i, 1, n) if (a[i] != a[i - 1]) v.push_back(a[i]);

	l = v.size();

	v.push_back(1e18);
	ll max_rest = 0;
	rep(i, 0, l - 1)
	{
		rep(j, 2, 2 * v[l - 1] / v[i])
		{
			ll rest = find(j*v[i]);
			rest = j*v[i]-a[rest];
			if (rest >= v[i]) continue;
			if (rest > max_rest) max_rest = rest;
		}
	}

	cout << max_rest;

	return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define all(v) v.begin(),v.end()
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int dx[] = { 1, 0, -1, 0 };
int dy[] = { 0, 1, 0, -1 };
int main(){
    /////////////////////////////////////////////////R_W;
    int n;
    cin >> n;
    vi data;
    r(n){
        int in;
        cin >> in;
        data.push_back(in);
    }
    int answer=0;
    sort(all(data));
    r(n){
        if (data[i] == 0) continue;
        for (int j = data[i]*2; j <= data.back()+data[i]; j+=data[i])
        {
            vector<int>::iterator it=upper_bound(all(data), j);
            if (it == data.begin()) continue;
            it--;
            if (it == data.begin()) continue;
            if (*it == j) it--;
                answer = max(answer, ((*it)%data[i]));
        }
        i = upper_bound(all(data), data[i]) - data.begin();
        i--;
    }
    cout << answer << endl;
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define all(v) v.begin(),v.end()
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int dx[] = { 1, 0, -1, 0 };
int dy[] = { 0, 1, 0, -1 };
int main(){
    /////////////////////////////////////R_W;
    int n;
    cin >> n;
    vi data;
    r(n){
        int in;
        cin >> in;
        data.push_back(in);
    }
    int answer=0;
    sort(all(data));
    r(n){
        if (data[i] == 0) continue;
        for (int j = data[i]*2; j <= data.back()*2; j+=data[i])
        {
            vector<int>::iterator it=upper_bound(all(data), j);
            if (it == data.begin()) continue;
            it--;
            if (it == data.begin()) continue;
            if (*it == j) it--;
                answer = max(answer, ((*it)%data[i]));
        }
        i = upper_bound(all(data), data[i]) - data.begin();
        i--;
    }
    cout << answer << endl;
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define all(v) v.begin(),v.end()
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int dx[] = { 1, 0, -1, 0 };
int dy[] = { 0, 1, 0, -1 };
int main(){
    //////////////////////////////////////////////R_W;
    int n;
    cin >> n;
    vi data;
    r(n){
        int in;
        cin >> in;
        data.push_back(in);
    }
    int answer=0;
    sort(all(data));
    r(n){
        for (int j = data[i]*2; j <= data.back()*2; j+=data[i])
        {
            vector<int>::iterator it=upper_bound(all(data), j);
            if (it == data.begin()) continue;
            it--;
            if (it == data.begin()) continue;
            if (*it == j) it--;
                answer = max(answer, ((*it)%data[i]));
        }
    }
    cout << answer << endl;
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define all(v) v.begin(),v.end()
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int dx[] = { 1, 0, -1, 0 };
int dy[] = { 0, 1, 0, -1 };
int main(){
    ////////////////////////////////R_W;
    int n;
    cin >> n;
    vi data;
    r(n){
        int in;
        cin >> in;
        data.push_back(in);
    }
    int answer=0;
    sort(all(data));
    r(n){
        if (data[i] == 0) continue;
        for (int j = data[i]*2; j <= data.back()*2; j+=data[i])
        {
            vector<int>::iterator it=upper_bound(all(data), j);
            if (it == data.begin()) continue;
            it--;
            if (it == data.begin()) continue;
            if (*it == j) it--;
                answer = max(answer, ((*it)%data[i]));
        }
    }
    cout << answer << endl;
    return 0;
}


input()
s = sorted(set(map(int, input().split())))
c = m = 0
d = []
for b in s:
    d += [c] * (b - c)
    c = b
for b in s[-1::-1]:
    if b < m + 2: break
    m = max(m, c % b, *(a % b for a in d[2 * b - 1::b]))
print(m)


input()
t = sorted(map(int, input().split()))[::-1]
s = 0
for i, a in enumerate(t, 1):
    if a < 2 * s + 2: break
    for b in t[i:]:
        if b < s + 2: break
        s = max(s, a % b)
print(s)


input()
t = sorted(map(int, input().split()))[::-1]
s = 0
for i, a in enumerate(t, 1):
    if a < 2 * s: break
    for b in t[i:]:
        if b < s + 2: break
        s = max(s, a % b)
print(s)


input()
t = sorted(map(int, input().split()))[::-1]
s = 0
for i, a in enumerate(t, 1):
    if a < 2 * s + 4: break
    for b in t[i:]:
        if b < s + 2: break
        s = max(s, a % b)
print(s)


input()
t = sorted(map(int, input().split()))
s = list(enumerate(t[1:]))
k = m = 0
while k < len(s):
    i, a = s[k]
    k += 1
    if a > 2 * m + 2 and t[i] > m + 1:
        m = max(m, a % t[i])
        if i: s.append((i - 1, a))
print(m)


input()
t = sorted(map(int, input().split()))
s = list(enumerate(t[1:]))[::-1]
k = m = 0
while k < len(s):
    i, a = s[k]
    k += 1
    if a > 2 * m + 2 and t[i] > m + 1:
        m = max(m, a % t[i])
        if i: s.append((i - 1, a))
print(m)


k, m, n = 0, 0, int(input()) - 1
t = sorted(map(int, input().split()))
s = list(zip(range(n), t[1:]))
while k < n:
    i, a = s[k]
    if a > 2 * m + 2 and t[i] > m + 1:
        m = max(m, a % t[i])
        s.append((i - 1, a))
        n += 1
    k += 1
print(m)


input()

s = sorted(set(map(int, input().split())))

c = m = 0

d = []

for b in s:

    d += [c] * (b - c)

    c = b

for b in s[-1::-1]:

    if b < m + 2: break

    m = max(m, c % b, *(a % b for a in d[2 * b - 1::b]))

print(m)

# Made By Mostafa_Khaled


n=int(input())
l=list(map(int,input().split()))
l=sorted(l)
k=0
ma=0
s=l[0]
r=0
for i in range(n-1) :
    k+=l[i+1]%l[i]
    while k>=s :
        k-=l[r+1]%l[r]
        r+=1
        s=l[r]
    ma=max(k,ma)
print(ma)


n=int(input())
l=list(map(int,input().split()))
l=sorted(l)
k=0
ma=0
s=l[0]
r=0
for i in range(n-1) :
    if l[i+1]%l[i]+k>=s :
        k=l[i+1]%l[i]
        r+=1
        s=l[r]
    else :
        k+=l[i+1]%l[i]
    ma=max(k,ma)
print(ma)


import java.util.Scanner;
import java.util.*;
public class code1 {

	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 int t=sc.nextInt();
		 while (--t>=0) {
			int n=sc.nextInt();
			int x=sc.nextInt();

			int[]z=new int[n];
			for (int i=0;i<n;i++) {
				z[i]=sc.nextInt();
				int v=1;
				int sum=0;
				if (z[i]>x) {
					for (int j=0;j*x<z[i];j++) {
						v=j;
					}
					sum=x*(v+1)-z[i];
					z[i]=sum;
				}
				else {
					sum=x*v-z[i];
				z[i]=sum;}
			}
		Arrays.sort(z);
		int j=0;
		int sum=0;
		int max=0;
		for (int i=0;i<n-1;i++) {
			if (z[i]==z[i+1]&&i!=n-2) {
				j++;
			}
			else {
				if (i==n-2&&z[i]==z[i+1])
					j++;
				sum=z[i]+j*x+1;
				if (z[i]!=z[i+1]&&i==n-2&&j==0)
				sum=z[i+1]+1;

				if (z[i]==0&&z[i+1]==0) {
					sum=0;
				}
				if (max <sum) {
					max=sum;
				}
				j=0;

			}

		}
		System.out.println(max);

		 }

		 }}


import java.util.Scanner;
import java.util.*;
public class code1 {

	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 int t=sc.nextInt();
		 while (--t>=0) {
			int n=sc.nextInt();
			long x=sc.nextInt();

			long[]z=new long[n];
			for (int i=0;i<n;i++) {
				z[i]=sc.nextInt();
				long v=1;
				long sum=0;
				if (z[i]>x) {
					for (int j=0;j*x<z[i];j++) {
						v=j;
					}
					sum=x*(v+1)-z[i];
					z[i]=sum;
				}
				else {
					sum=x*v-z[i];
				z[i]=sum;}
			}
		Arrays.sort(z);
		int j=0;
		long sum=0;
		long max=0;
		for (int i=0;i<n-1;i++) {
			if (z[i]==z[i+1]&&i!=n-2&&z[i]!=0) {
				j++;
			}
			else {

				if (i==n-2&&z[i]==z[i+1])
					j++;
				sum=z[i]+j*x+1;
				if (z[i]!=z[i+1]&&i==n-2)
					if (sum<z[i+1]+1)
						sum=z[i+1]+1;

				if (z[i]==0&&z[i+1]==0) {
					sum=0;
				}
				if (max <sum) {
					max=sum;
				}
				j=0;

			}

		}
		if (n==1)
			max=z[0];
		System.out.println(max+1);

		 }

		 }}


import java.util.Scanner;
import java.util.*;
public class code1 {

	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 int t=sc.nextInt();
		 while (--t>=0) {
			int n=sc.nextInt();
			int x=sc.nextInt();

			int[]z=new int[n];
			for (int i=0;i<n;i++) {
				z[i]=sc.nextInt();
				int v=1;
				int sum=0;
				if (z[i]>x) {
					for (int j=0;j*x<z[i];j++) {
						v=j;
					}
					sum=x*(v+1)-z[i];
					z[i]=sum;
				}
				else {
					sum=x*v-z[i];
				z[i]=sum;}
			}
		Arrays.sort(z);
		int j=0;
		int sum=0;
		int max=0;
		for (int i=0;i<n-1;i++) {
			if (z[i]==z[i+1]&&i!=n-2) {
				j++;
			}
			else {
				if (i==n-2&&z[i]==z[i+1])
					j++;
				sum=z[i]+j*x+1;
				if (z[i]!=z[i+1]&&i==n-2)
				sum=z[i+1]+1;

				if (z[i]==0) {
					sum=0;
				}
				if (max <sum) {
					max=sum;
				}
				j=0;

			}

		}
		System.out.println(max);

		 }

		 }}


import java.util.Scanner;
import java.util.*;
public class code1 {

	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 int t=sc.nextInt();
		 while (--t>=0) {
			int n=sc.nextInt();
			long x=sc.nextInt();

			long[]z=new long[n];
			for (int i=0;i<n;i++) {
				z[i]=sc.nextInt();
				long v=1;
				long sum=0;
				if (z[i]>x) {
					for (int j=0;j*x<z[i];j++) {
						v=j;
					}
					sum=x*(v+1)-z[i];
					z[i]=sum;
				}
				else {
					sum=x*v-z[i];
				z[i]=sum;}
			}
		Arrays.sort(z);
		int j=0;
		long sum=0;
		long max=0;
		for (int i=0;i<n-1;i++) {
			if (z[i]==z[i+1]&&i!=n-2&&z[i]!=0) {
				j++;
			}
			else {

				if (i==n-2&&z[i]==z[i+1])
					j++;
				sum=z[i]+j*x+1;
				if (z[i]!=z[i+1]&&i==n-2)
					if (sum<z[i+1]+1)
						sum=z[i+1]+1;

				if (z[i]==0&&z[i+1]==0) {
					sum=0;
				}
				if (max <sum) {
					max=sum;
				}
				j=0;

			}

		}
		if (n==1&&z[0]!=0)

			max=z[0]+1;
		System.out.println(max);

		 }

		 }}


import java.util.Scanner;
import java.util.*;
public class code1 {

	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 int t=sc.nextInt();
		 while (--t>=0) {
			int n=sc.nextInt();
			long x=sc.nextInt();

			long[]z=new long[n];
			for (int i=0;i<n;i++) {
				z[i]=sc.nextInt();
				long v=1;
				long sum=0;
				if (z[i]>x) {
					for (int j=0;j*x<z[i];j++) {
						v=j;
					}
					sum=x*(v+1)-z[i];
					z[i]=sum;
				}
				else {
					sum=x*v-z[i];
				z[i]=sum;}
			}
		Arrays.sort(z);
		int j=0;
		long sum=0;
		long max=0;
		for (int i=0;i<n-1;i++) {
			if (z[i]==z[i+1]&&i!=n-2&&z[i]!=0) {
				j++;
			}
			else {

				if (i==n-2&&z[i]==z[i+1])
					j++;
				sum=z[i]+j*x+1;
				if (z[i]!=z[i+1]&&i==n-2)
					if (sum<z[i+1]+1)
						sum=z[i+1]+1;

				if (z[i]==0&&z[i+1]==0) {
					sum=0;
				}
				if (max <sum) {
					max=sum;
				}
				j=0;

			}

		}
		if (n==1)
			max=z[0]+1;
		System.out.println(max);

		 }

		 }}


import java.util.Scanner;
import java.util.*;
public class code1 {

	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 int t=sc.nextInt();
		 while (--t>=0) {
			int n=sc.nextInt();
			int x=sc.nextInt();

			int[]z=new int[n];
			for (int i=0;i<n;i++) {
				z[i]=sc.nextInt();
				int v=1;
				int sum=0;
				if (z[i]>x) {
					for (int j=0;j*x<z[i];j++) {
						v=j;
					}
					sum=x*(v+1)-z[i];
					z[i]=sum;
				}
				else {
					sum=x*v-z[i];
				z[i]=sum;}
			}
		Arrays.sort(z);
		int j=0;
		int sum=0;
		int max=0;
		for (int i=0;i<n-1;i++) {
			if (z[i]==z[i+1]&&i!=n-2) {
				j++;
			}
			else {
				if (i==n-2&&z[i]==z[i+1])
					j++;
				sum=z[i]+j*x+1;
				if (z[i]!=z[i+1]&&i==n-2)
				sum=z[i+1]+1;

				if (z[i]==0&&z[i+1]==0) {
					sum=0;
				}
				if (max <sum) {
					max=sum;
				}
				j=0;

			}

		}
		System.out.println(max);

		 }

		 }}


import java.util.Scanner;
import java.util.*;
public class code1 {

	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 int t=sc.nextInt();
		 while (--t>=0) {
			int n=sc.nextInt();
			long x=sc.nextInt();

			long[]z=new long[n];
			for (int i=0;i<n;i++) {
				z[i]=sc.nextInt();
				long v=1;
				long sum=0;
				if (z[i]>x) {
					for (int j=0;j*x<z[i];j++) {
						v=j;
					}
					sum=x*(v+1)-z[i];
					z[i]=sum;
				}
				else {
					sum=x*v-z[i];
				z[i]=sum;}
			}
		Arrays.sort(z);
		int j=0;
		long sum=0;
		long max=0;
		for (int i=0;i<n-1;i++) {
			if (z[i]==z[i+1]&&i!=n-2&&z[i]!=0) {
				j++;
			}
			else {

				if (i==n-2&&z[i]==z[i+1])
					j++;
				sum=z[i]+j*x+1;
				if (z[i]!=z[i+1]&&i==n-2)
					if (sum<z[i+1]+1)
						sum=z[i+1]+1;

				if (z[i]==0&&z[i+1]==0) {
					sum=0;
				}
				if (max <sum) {
					max=sum;
				}
				j=0;

			}

		}
		System.out.println(max);

		 }

		 }}


import java.util.Scanner;
import java.util.*;
public class code1 {

	public static void main(String[] args) {
		 Scanner sc = new Scanner(System.in);
		 int t=sc.nextInt();
		 while (--t>=0) {
			int n=sc.nextInt();
			int x=sc.nextInt();

			int[]z=new int[n];
			for (int i=0;i<n;i++) {
				z[i]=sc.nextInt();
				int v=1;
				int sum=0;
				if (z[i]>x) {
					for (int j=0;j*x<z[i];j++) {
						v=j;
					}
					sum=x*(v+1)-z[i];
					z[i]=sum;
				}
				else {
					sum=x*v-z[i];
				z[i]=sum;}
			}
		Arrays.sort(z);
		int j=0;
		int sum=0;
		int max=0;
		for (int i=0;i<n-1;i++) {
			if (z[i]==z[i+1]&&i!=n-2&&z[i]!=0) {
				j++;
			}
			else {

				if (i==n-2&&z[i]==z[i+1])
					j++;
				sum=z[i]+j*x+1;
				if (z[i]!=z[i+1]&&i==n-2)
					if (sum<z[i+1]+1)
						sum=z[i+1]+1;

				if (z[i]==0&&z[i+1]==0) {
					sum=0;
				}
				if (max <sum) {
					max=sum;
				}
				j=0;

			}

		}
		System.out.println(max);

		 }

		 }}


import java.util.*;

public class ZeroArray {

    public long n;
    public long k;
    public long[] a;

    public ZeroArray(long n, long k, long[] a) {
        this.n = n;
        this.k = k;
        this.a = a;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        long N = scan.nextLong();

        for (long i = 0; i < N; i++) {
            long n = scan.nextLong();
            long k = scan.nextLong();
            long[] a = new long[(int) n];
            for (long j = 0; j < n; j++) {
                a[(int) j] = scan.nextLong();
            }
            ZeroArray p = new ZeroArray(n, k, a);
            System.out.println(p.solve());
        }
    }

    public long solve() {

        // Calculate dif array
        long[] dif = new long[(int) n];
        PriorityQueue<Long> pq = new PriorityQueue<>();

        for (int i = 0; i < dif.length; i++) {
            dif[i] = (a[i] % k) == 0 ? 0 : k - (a[i] % k);
            pq.add(dif[i]);
        }

        long x = 0;
        long numMoves = 0;
        while (!pq.isEmpty()) {
            long nextdif = pq.poll();
            if (nextdif != 0) {

                numMoves += nextdif - x;
                numMoves++;
                x = nextdif + 1;

                Long skipdif = pq.peek();
                long m = 1;
                while (skipdif != null && skipdif == nextdif) {
                    long movedif = pq.poll();
                    pq.add(movedif + m * k);
                    m++;
                    skipdif = pq.peek();
                }
            }
        }

        return numMoves;
    }
}


import java.util.*;

public class ZeroArray {

    public int n;
    public int k;
    public int[] a;

    public ZeroArray(int n, int k, int[] a) {
        this.n = n;
        this.k = k;
        this.a = a;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();

        for (int i = 0; i < N; i++) {
            int n = scan.nextInt();
            int k = scan.nextInt();
            int[] a = new int[n];
            for (int j = 0; j < n; j++) {
                a[j] = scan.nextInt();
            }
            ZeroArray p = new ZeroArray(n, k, a);
            System.out.println(p.solve());
        }
    }

    public int solve() {

        // Calculate dif array
        int[] dif = new int[n];
        PriorityQueue<Integer> pq = new PriorityQueue<>();

        for (int i = 0; i < dif.length; i++) {
            dif[i] = (a[i] % k) == 0 ? 0 : k - (a[i] % k);
            pq.add(dif[i]);
        }

        int x = 0;
        int numMoves = 0;
        while (!pq.isEmpty()) {
            int nextdif = pq.poll();
            if (nextdif == x) {

                if (nextdif != 0) {

                    numMoves++;
                    x++;
                }

            } else if (nextdif > x) {

                numMoves += nextdif - x;
                numMoves++;
                x = nextdif + 1;

            } else {

                int skipdif = nextdif + k;
                pq.add(skipdif);

            }
        }

        return numMoves;
    }
}