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++ (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<cstring>
using namespace std;

int v[100005];
int n,x,y,tot=0,h[100005];
int scc[100005],dfn[100005],low[100005],C,cnt;
bool vis[100005];
struct Edge{
	int x,next;
}e[5000000];
pair<int,int> a[20005];
stack<int> s;

inline void add_edge(int x,int y){
	e[++tot].x=y;
	e[tot].next=h[x],h[x]=tot;
}

void tarjan(int x){
	dfn[x]=low[x]=++cnt;
	s.push(x),vis[x]=true;
	for(int i=h[x];~i;i=e[i].next){
		if(!dfn[e[i].x]){
			tarjan(e[i].x),low[x]=min(low[x],low[e[i].x]);
		}else if(vis[e[i].x]){
			low[x]=min(low[x],dfn[e[i].x]);
		}   
	}
	if(dfn[x]==low[x]){
		C++;
		int tmp;
		for(;;){
			tmp=s.top();
			vis[tmp]=false,scc[tmp]=C;
			s.pop();
			if(tmp==x)break;
		}
	}
}

void build(int o,int l,int r){
	if(r-l==1){
		add_edge(o+n*2,a[l].second^1);
		return;
	}
	add_edge(o+n*2,(o<<1)+n*2);
	add_edge(o+n*2,(o<<1|1)+n*2);
	int mid=l+r>>1;
	build(o<<1,l,mid),build(o<<1|1,mid,r);
}

void link(int o,int l,int r,int x,int y,int a){
	if(l>=x&&r<=y){
		add_edge(a,o+n*2);
		return;
	}
	int mid=l+r>>1;
	if(x<mid)link(o<<1,l,mid,x,y,a);
	if(y>mid)link(o<<1|1,mid,r,x,y,a);
}

inline pair<int,int> get(int x,int len){
	int l=0,r=x;
	pair<int,int> ans;
	while(l<r){
		int mid=l+r>>1;
		if(a[x].first-a[mid].first<len)r=mid;
		else l=mid+1;
	}
	ans.first=l;
	l=x,r=n*2-1;
	while(l<r){
		int mid=l+r+1>>1;
		if(a[mid].first-a[x].first<len)l=mid;
		else r=mid-1;
	}
	ans.second=l;
	return ans;
}

inline bool check(int d){
	memset(scc,0,sizeof(scc));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(h,-1,sizeof(h)),tot=0;
	build(1,0,n*2);
	for(int i=0;i<n*2;i++){
		int id=a[i].second;
		pair<int,int> x=get(i,d);
		if(i<x.second)link(1,0,n*2,i+1,x.second+1,id);
		if(x.first<i)link(1,0,n*2,x.first,i,id);
	}
	C=cnt=0;
	for(int i=0;i<n*2;i++)if(!dfn[i])tarjan(i);
	for(int i=0;i<n*2;i++)
		if(scc[a[i].second]==scc[a[i].second^1])return false;
	return true;
}

int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d%d",&x,&y);
		a[2*i+1]=make_pair(x,2*i+1);
		a[2*i]=make_pair(y,2*i);
	}
	sort(a,a+n*2);
	int l=0,r=1000000000;
	while(l<r){
		int mid=l+r+1>>1;
		if(check(mid))l=mid;
		else r=mid-1;
	}
	printf("%d",l);
}