Problem:
Score : 1200 points

Problem Statement

Snuke loves flags.

Snuke is placing N flags on a line.

The i-th flag can be placed at either coordinate x_i or coordinate y_i.

Snuke thinks that the flags look nicer when the smallest distance between two of them, d, is larger. Find the maximum possible value of d.

Constraints

2 ≤ N ≤ 10^{4}

1 ≤ x_i, y_i ≤ 10^{9}

x_i and y_i are integers.

Input

The input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the answer.

Sample Input 1

3
1 3
2 5
1 9

Sample Output 1

4

The optimal solution is to place the first flag at coordinate 1, the second flag at coordinate 5 and the third flag at coordinate 9. The smallest distance between two of the flags is 4 in this case.

Sample Input 2

5
2 2
2 2
2 2
2 2
2 2

Sample Output 2

0

There can be more than one flag at the same position.

Sample Input 3

22
93 6440
78 6647
862 11
8306 9689
798 99
801 521
188 206
6079 971
4559 209
50 94
92 6270
5403 560
803 83
1855 99
42 504
75 484
629 11
92 122
3359 37
28 16
648 14
11 269

Sample Output 3

17

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define ls (p << 1)
#define rs ((p << 1) | 1)
using namespace std;
const int N = 2e5+5 , inf = 1e9;
inline int read()
{
	int x = 0 , f = 1;	char ch = getchar();
	while(ch < '0' || ch > '9')	{if(ch == '-')	f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
	return x * f;
}
int n , tot , cnt , top , tp;
int st[N * 5] , low[N * 5] , pos[N] , belong[N * 5] , dfn[N * 5] , head[N * 5]; 
bool vis[N * 5]; 
struct card 
{
	int val,id;
	friend bool operator < (const card &a,const card &b) {return a.val < b.val;}
}e[N];
struct Edge {int to , nxt;}cur[N * 15];
inline void add(int u,int v)
{
	cur[++cnt] = (Edge){v,head[u]};
	head[u] = cnt;
}
#define mid ((l + r) >> 1)
void build(int p,int l,int r)
{
	if(l == r) return (void)(add(p + (n << 1),e[l].id <= n ? e[l].id + n : e[l].id - n));
	build(ls,l,mid); build(rs,mid+1,r);
	add(p + (n << 1),ls + (n << 1));
	add(p + (n << 1),rs + (n << 1));
}
void chenge(int p,int l,int r,int x,int y,int z) 
{
	if(x <= l && r <= y) return (void)(add(z,p + (n << 1)));
	if(x <= mid) chenge(ls,l,mid,x,y,z);
	if(y > mid) chenge(rs,mid+1,r,x,y,z);
}
#undef mid
#define to cur[i].to
void tarjan(int x) 
{
	dfn[x] = low[x] = ++tot;
	vis[x] = 1;
	st[++top] = x;
	for (int i = head[x]; i; i = cur[i].nxt)
	if (!dfn[to]) 
	{
		tarjan(to);
		low[x] = min(low[x], low[to]);
	} 
	else if (vis[to])low[x] = min(low[x], dfn[to]);
	if (dfn[x] == low[x]) 
	{
		cnt++;
		while (st[top] != x) 
		{
			belong[st[top]] = cnt;
			vis[st[top]] = 0;
			top--;
		}
		belong[x] = cnt;
		vis[x] = 0;
		top--;
	}
}
#undef to
inline bool check(int mid)
{
	tot = top = 0; cnt = 0;
	memset(cur,0,sizeof cur);
	memset(head,0,sizeof head);
	memset(dfn,0,sizeof dfn);
	build(1,1,n << 1);
	int l = 1 , r = 1;
	for(int i = 1 , id , opt;i <= (n << 1);i ++)
	{
		id = e[i].id;
		if(id <= n) opt = pos[id + n];
		else opt = pos[id - n];
		while(r <= i && e[r].val <= e[i].val - mid) r ++;
		if(r < i && r >= 1 && e[r].val > e[i].val - mid)
		{
			if(opt >= r && opt < i)
			{
				if(opt > r) chenge(1,1,(n << 1),r,opt-1,e[i].id);
				if(opt < i - 1) chenge(1,1,(n << 1),opt+1,i-1,e[i].id);
			}
			else chenge(1,1,(n << 1),r,i-1,e[i].id);
		}
		while(l <= (n << 1) && e[l].val < e[i].val + mid) l ++;
		l --;
		if(l > i && l <= (n << 1) && e[l].val < e[i].val + mid)
		{
			if(opt > i && opt <= l)
			{
				if(opt > i + 1) chenge(1,1,(n << 1),i+1,opt-1,e[i].id);
				if(opt < l) chenge(1,1,(n << 1),opt+1,l,e[i].id);
			}
			else chenge(1,1,(n << 1),i+1,l,e[i].id);
		} 
	}
	cnt = 0;
	for(int i = 1;i <= (n << 1);i ++) if(!dfn[i]) tarjan(i); 
	for(int i = 1;i <= n;i ++) if(belong[i] == belong[i+n]) return 0;
	return 1;
}
int main()
{
	// freopen("disaster.in","r",stdin);
	// freopen("disaster.out","w",stdout);
	n = read(); int minn = inf , maxn = -inf;
	for(int i = 1 , x , y;i <= n;i ++) 
	{
		x = read();  y = read();
		e[++cnt] = (card){x,i};	e[++cnt] = (card){y,i+n};
		minn = min(minn,min(x,y)); maxn = max(maxn,max(x,y));
	}
	if(maxn - minn + 1 < n) 
	{
		puts("0");
		fclose(stdin);
		fclose(stdout);
		return 0;
	}
	sort(e+1,e+1+cnt);
	for(int i = 1;i <= (n << 1);i ++) pos[e[i].id] = i;
	int l = 0 , r = 1e9 , ans;
	while(l <= r)
	{
		int mid = (l + r) >> 1;
		if(check(mid)) ans = mid , l = mid + 1;
		else r = mid - 1;
	}
	printf("%d\n",ans);
	fclose(stdin);
	fclose(stdout);
	return 0;
}
/*
3
1 2
3 4
5 6
*/