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<cstring>
#include<algorithm>
using namespace std;
const int N=20010,M=2000010,NN=80010;
struct position{
	int pos,id;
}pt[N];
struct node{
	int f;
}tree[NN];
struct edge{
	int y,next;
}data[M];
int n,l,r,num,num1,num2,top,sta[NN],h[NN],dfn[NN],low[NN],arc[NN];
bool vis[NN],insta[NN];
inline int read(){
	int x=0,f=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return f?-x:x;
}
inline void add(int x,int y){
	data[++num].y=y;data[num].next=h[x];h[x]=num;
}
bool cmp(position i,position j){
	return i.pos<j.pos;
}
inline int get_front(int pos,int a,int b){
	while(a<=b){
		int mid=(a+b)>>1;
		if(pt[mid].pos<pos)a=mid+1;
		else b=mid-1;
	}
	return a;
}
inline int get_back(int pos,int a,int b){
	while(a<=b){
		int mid=(a+b)>>1;
		if(pt[mid].pos>pos)b=mid-1;
		else a=mid+1;
	}
	return b;
}
void build(int p,int a,int b){
	if(a==b){arc[pt[a-1].id]=p;return;}
	int mid=(a+b)>>1;
	build(p<<1,a,mid);build(p<<1|1,mid+1,b);
	add(p<<1,p);add(p<<1|1,p);
}
void addedge(int p,int pa,int pb,int a,int b,int x){
	if(a<=pa&&pb<=b){add(p,x);return;}
	int mid=(pa+pb)>>1;
	if(a<=mid)addedge(p<<1,pa,mid,a,b,x);
	if(mid<b)addedge(p<<1|1,mid+1,pb,a,b,x);
}
void tarjan(int u){
	sta[++top]=u;
	insta[u]=vis[u]=true;
	dfn[u]=low[u]=++num1;
	for(int i=h[u];i!=-1;i=data[i].next){
		int v=data[i].y;
		if(!dfn[v]){tarjan(v);low[u]=min(low[u],low[v]);}
		else if(insta[v]&&low[u]>dfn[v])low[u]=dfn[v];
	}
	if(dfn[u]==low[u]){
		num2++;
		while(dfn[sta[top]]!=low[sta[top]]){
			tree[sta[top]].f=num2;insta[sta[top--]]=false;
		}
		tree[u].f=num2;insta[sta[top--]]=false;
	}
}
bool two_sat(int x){
	memset(h,-1,sizeof h);num=num1=num2=top=0;
	build(1,1,n+n);
	for(int l1,r1,i=0;i<n+n;i++){
		l1=get_front(pt[i].pos-x+1,0,i-1);
		r1=get_back(pt[i].pos+x-1,i+1,n+n-1);
		if(l1<i)addedge(1,1,n+n,l1+1,i,arc[pt[i].id^1]);
		if(i<r1)addedge(1,1,n+n,i+2,r1+1,arc[pt[i].id^1]);
	}
	memset(dfn,0,sizeof dfn);
	memset(vis,false,sizeof vis);
	for(int i=0;i<n+n;i++)if(!vis[arc[i]])tarjan(arc[i]);
	for(int i=0;i<n;i++)if(tree[arc[i<<1]].f==tree[arc[i<<1|1]].f)return false;
	return true;
}
int main(){
	n=read();
	for(int i=0;i<n;i++){
		pt[i<<1].pos=read();pt[i<<1].id=i<<1;
		pt[i<<1|1].pos=read();pt[i<<1|1].id=i<<1|1;
	}
	sort(pt,pt+n+n,cmp);
	l=0;r=1000000000;
	while(l<=r){
		int mid=(l+r)>>1;
		if(two_sat(mid))l=mid+1;
		else r=mid-1;
	}
	printf("%d",r);
	return 0;
}