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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;

typedef tree <pii, null_type, less <pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int N = 1e4 + 123, M = 1e5 + 123, INF = (int)1e9 + 1;
int n, k;
pii flag[N];
vi g[M];

vector <pii> p;

void add_or(int x, int y) {
	g[2 * x].pb(2 * y - 1);
	g[2 * y].pb(2 * x - 1);
}

void add_nand(int x, int y) {
	g[2 * x - 1].pb(2 * y);
	g[2 * y - 1].pb(2 * x);
}

void add(int x, int y) {
	g[2 * x].pb(2 * y);
	g[2 * y - 1].pb(2 * x - 1);
}

struct node {
	int l = 0, r = 0, ind = 0;
};

node t[(1 << 16) + 1];

void build(int v, int l, int r) {
	t[v].l = l, t[v].r = r;
	if (l == r) {
		t[v].ind = p[l - 1].S;
		return;
	}	else {
		t[v].ind = ++k;
	}
	int m = l + r >> 1;
	build(v << 1, l, m);
	build(v << 1 | 1, m + 1, r);
	add(t[v].ind, t[v << 1].ind);
	add(t[v].ind, t[v << 1 | 1].ind);
}

void upd(int v, int l, int r, int x) {
	if (t[v].l > r || t[v].r < l) {
		return;
	}
	if (t[v].l >= l && t[v].r <= r) {
		add_nand(x, t[v].ind);
		return;
	}
	upd(v << 1, l, r, x);
	upd(v << 1 | 1, l, r, x);
}

void process(int x, int ind, int y) {
	int l = lower_bound(all(p), mp(x, 0)) - p.begin() + 1;
	int r = upper_bound(all(p), mp(x + y - 1, INF)) - p.begin();
	
	int m = lower_bound(all(p), mp(x, ind)) - p.begin() + 1;
	if (l <= m - 1) {
		upd(1, l, m - 1, ind);
	}
	if (m + 1 <= r) {
		upd(1, m + 1, r, ind);
	}
}

vi gr[M], ord;
int used[M], cnt, comp[M];

void dfs(int v) {
	used[v] = 1;
	for (int to : g[v]) {
		if (used[to]) {
			continue;
		}
		dfs(to);
	}
	ord.pb(v);
}

void go(int v) {
	used[v] = 1;
	comp[v] = cnt;
	for (int to : gr[v]) {
		if (used[to]) {
			continue;
		}
		go(to);
	}
}

bool check(int x) {
	for (int i = 1; i <= 2 * k; i++) {
		g[i].clear();
		gr[i].clear();
	}
	k = 2 * n;
	build(1, 1, 2 * n);
	for (int i = 1; i <= n; i++) {
		add_or(2 * i - 1, 2 * i);
		process(flag[i].F, 2 * i - 1, x);
		process(flag[i].S, 2 * i, x);
	}
	for (int i = 1; i <= 2 * k; i++) {
		for (int to : g[i]) {
			gr[to].pb(i);
		}
	}
	memset(& used, 0, sizeof(used));
	ord.clear();
	for (int i = 1; i <= 2 * k; i++) {
		if (!used[i]) {
			dfs(i);
		}
	}
	memset(& used, 0, sizeof(used));
	
	reverse(all(ord));
	memset(& comp, 0, sizeof(comp));
	cnt = 0;
	for (int i : ord) {
		if (!used[i]) {
			cnt++;
			go(i);
		}
	}
	for (int i = 2; i <= 2 * k; i += 2) {
		if (comp[i] == comp[i - 1]) {
			return false;
		}
	}
	return true;
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(NULL);
	#ifdef LOCAL
		freopen("input.txt", "r", stdin);
	#endif
	
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> flag[i].F >> flag[i].S;
		p.pb({flag[i].F, 2 * i - 1});
		p.pb({flag[i].S, 2 * i});
	}
	sort(all(p));
	int l = 0, r = INF;
	while (l < r - 1) {
		int mid = l + r >> 1;
		if (check(mid)) {
			l = mid;
		}	else {
			r = mid;
		}
	}
	cout << l;
}