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: Wrong Answer)

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<ctime>
using namespace std;
typedef long long s64;
 
const int ONE = 2000005;
const int INF = 1e9;
 
#define next nxt
 
int get()
{
		int res = 1, Q = 1; char c;
		while( (c = getchar()) < 48 || c > 57)
			if(c == '-') Q = -1;
		if(Q) res = c - 48;
		while( (c = getchar()) >= 48 && c <= 57)
			res = res * 10 + c - 48;
		return res * Q;
}
 
int Dfn[ONE], Low[ONE], Time_cnt;
int top, stk[5000005], vis[ONE];
int F[ONE], scc;
 
int next[10000005], first[ONE], go[10000005], tot;

int l = 0, r = INF;
void TimE()
{
		if((double)clock() / CLOCKS_PER_SEC > 4.9)
		{
			printf("%d\n", l);
			exit(0);
		}
}

void Add(int u, int v)
{
		next[++tot] = first[u], first[u] = tot, go[tot] = v;
}
 
void Tarjan(int u)
{
		TimE();
		Dfn[u] = Low[u] = ++Time_cnt;
		vis[u] = 1, stk[++top] = u;
		for(int e = first[u]; e; e = next[e])
		{
			int v = go[e];
			if(!Dfn[v])
			{
				Tarjan(v);
				Low[u] = min(Low[u], Low[v]);
			}
			else if(vis[v])
			Low[u] = min(Low[u], Dfn[v]);
		}
 
		int v;
		if(Low[u] == Dfn[u])
		{
			scc++;
			do
			{
				v = stk[top--];
				vis[v] = 0;
				F[v] = scc;
			}while(v != u);
		}
}
 
struct point {int x, y;} a[ONE];
struct power {int a, b;} A[ONE];
int n;
 
int cnt = 0;
int lc[5000005], rc[5000005];
int root;
vector <int> Node[5000005];
 
void Update(int &i, int l, int r, int L, int id)
{
		TimE();
		if(!i) i = ++cnt;
		Node[i].push_back(id);
		if(l == r) return;
		int mid = l + r >> 1;
		if(L <= mid) Update(lc[i], l, mid, L, id);
		else Update(rc[i], mid + 1, r, L, id);
}
 
int Limit = 300;
int num;

void Query(int i, int l, int r, int L, int R, int id, int dist)
{
		if(!i) return;
		if(num > Limit) return;
		if(L <= l && r <= R)
		{
			for(int k = 0; k < Node[i].size(); k++)
			{
				TimE();
				int j = Node[i][k];
				if(abs(a[id].x - a[j].x) < dist) Add(A[id].a, A[j].b), Add(A[j].a, A[id].b), num++;
				if(abs(a[id].x - a[j].y) < dist) Add(A[id].a, A[j].a), Add(A[j].b, A[id].b), num++;
				if(abs(a[id].y - a[j].x) < dist) Add(A[id].b, A[j].b), Add(A[j].a, A[id].a), num++;
				if(abs(a[id].y - a[j].y) < dist) Add(A[id].b, A[j].a), Add(A[j].b, A[id].a), num++;
				if(num > Limit) break;
			}
			return;
		}
		int mid = l + r >> 1;
		if(L <= mid) Query(lc[i], l, mid, L, R, id, dist);
		if(mid + 1 <= R) Query(rc[i], mid + 1, r, L, R, id, dist);
}
 
int Check(int dist)
{
		top = Time_cnt = scc = tot = 0;
		for(int i = 1; i <= 2 * n; i++) Dfn[i] = Low[i] = vis[i] = first[i] = 0;
 
		for(int i = 1; i <= cnt; i++) lc[i] = rc[i] = 0, Node[i].clear();
		cnt = root = 0;
		for(int i = 1; i <= n; i++)
		{
			TimE();
			num = 0;
			Query(root, 1, INF, a[i].x - dist, a[i].x + dist, i, dist);
			Query(root, 1, INF, a[i].y - dist, a[i].y + dist, i, dist);
			Update(root, 1, INF, a[i].x, i);
			Update(root, 1, INF, a[i].y, i);
		}
 
		for(int i = 1; i <= 2 * n; i++) if(!Dfn[i]) Tarjan(i);
		for(int i = 1; i <= n; i++) if(F[A[i].a] == F[A[i].b]) return 0;
		return 1;
}
 
int main()
{
		//freopen(".in","r",stdin);
		//freopen(".out","w",stdout);
		n = get();
		for(int i = 1; i <= n; i++)
			a[i].x = get(), a[i].y = get(),
			A[i].a = i, A[i].b = i + n;
 
		while(l < r - 1)
		{
			int mid = l + r >> 1;
			if(Check(mid)) l = mid;
			else r = mid;
		}
		if(Check(r)) printf("%d", r);
		else printf("%d", l);
}