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 <stack>
#include <set>
#include <unistd.h>

using namespace std;

typedef pair<int,int> ii;

const int MAXN = 10004;

int n,k;
int p[MAXN*2];
stack<int> s;
set<ii> nodes;
bool visited[MAXN*2];
int scc[MAXN*2];

int other(int a){
    if(a>=MAXN) return a-MAXN;
    return a+MAXN;
}

void dfs1(int node){
    if(visited[node]) return;
    visited[node]=true;
    auto it=nodes.upper_bound(ii(p[node]-k, 2*MAXN));
    for(;it!=nodes.end() && it->first<p[node]+k;){
        if(it->second==node){
            it++;
        }else{
            int nxt=it->second;
            nodes.erase(it);
            dfs1(other(nxt));
            it=nodes.upper_bound(ii(p[node]-k, 2*MAXN));
        }
    }
    s.push(node);
}

void dfs2(int node, int start){
    if(scc[node]!=-1) return;
    scc[node]=start;
    auto it=nodes.upper_bound(ii(p[other(node)]-k, 2*MAXN));
    for(;it!=nodes.end() && it->first<p[other(node)]+k;){
        if(it->second==other(node)){
            it++;
        }else{
            int nxt=it->second;
            nodes.erase(it);
            dfs2(nxt, start);
            it=nodes.upper_bound(ii(p[other(node)]-k, 2*MAXN));
        }
    }
}


bool ok(int g){
    k=g;
    nodes.clear();
    for(int i=0;i<n;++i){
        nodes.emplace(p[i], i);
        nodes.emplace(p[MAXN+i], MAXN+i);
    }
    fill(visited, visited+MAXN*2, false);
    fill(scc, scc+MAXN*2, -1);
    for(int i=0;i<n;++i){
        if(!visited[i]){
            dfs1(i);
        }
        if(!visited[i+MAXN]){
            dfs1(i+MAXN);
        }
    }
    nodes.clear();
    for(int i=0;i<n;++i){
        nodes.emplace(p[i], i);
        nodes.emplace(p[MAXN+i], MAXN+i);
    }
    while(!s.empty()){
        int i=s.top();
        s.pop();
        if(scc[i]==-1) dfs2(i, i);
    }
    for(int i=0;i<n;++i){
        if(scc[i]==scc[MAXN+i]){
            return false;
        }
    }
    return true;
}


int main(){
    scanf("%d", &n);
    for(int i=0;i<n;++i){
        scanf("%d%d", &p[i], &p[i+MAXN]);
    }
    int lo=0,hi=1000000009;
    while(lo<hi){
        int mid=lo+(hi-lo+1)/2;
        if(ok(mid)){
            lo=mid;
        }else{
            hi=mid-1;
        }
    }
    printf("%d", lo);
}