Problem:
Score : 1200 points

Problem Statement

Let M be a positive integer.

You are given 2 N integers a_1, a_2, \ldots, a_{2 N}, where 0 \leq a_i < M for each i.

Consider dividing the 2 N integers into N pairs.
Here, each integer must belong to exactly one pair.

We define the ugliness of a pair (x, y) as (x + y) \mod M.
Let Z be the largest ugliness of the N pairs. Find the minimum possible value of Z.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^9

0 \leq a_i < M

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 \cdots a_{2N}

Output

Print the minimum possible value of Z, where Z is the largest ugliness of the N pairs.

Sample Input 1

3 10
0 2 3 4 5 9

Sample Output 1

5

One solution is to form pairs (0, 5), (2, 3) and (4, 9), with ugliness 5, 5 and 3, respectively.

Sample Input 2

2 10
1 9 1 9

Sample Output 2

0

Pairs (1, 9) and (1, 9) should be formed, with ugliness both 0.

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#define inf (int)(2e9)

signed compare(const void *a, const void *b){
	int sub = *(int *)a - *(int *)b;
	if(sub < 0){
		return -1;
	}
	else if(sub == 0){
		return 0;
	}
	else{
		return 1;
	}
}

int max(int a, int b){
	return a >= b ? a : b;
}

int min(int a, int b){
	return a <= b ? a : b;
}

int sub1(int N, int *a, int K){
	int i, ans = 0;
	for(i = 0; i < K; i++){
		ans = max(ans, a[i] + a[2 * K - 1 - i]);
	}
	return ans;
}

int sub2(int N, int *a, int L){
	int i, ans = inf;
	for(i = 0; i < L; i++){
		ans = min(ans, a[2 * N - 1 - i] + a[2 * N - 2 * L + i]);
	}
	return ans;
}

int sub3(int N, int *a, int L){
	int i, ans = 0;
	for(i = 0; i < L; i++){
		ans = max(ans, a[2 * N - 1 - i] + a[2 * N - 2 * L + i]);
	}
	return ans;
}

int can_achieve(int N, int M, int *a, int x){
//	printf("x = %d\n", x);
	int l, h, r;
	int v1, v2, v3;

	l = -1;
	r = N + 1;
	while(r - l > 1){
		h = (l + r) / 2;
		if(sub1(N, a, h) > x){
			r = h;
		}
		else{
			l = h;
		}
	}
	v1 = l;

	l = -1;
	r = N + 1;
	while(r - l > 1){
		h = (l + r) / 2;
		if(sub2(N, a, h) >= M){
			l = h;
		}
		else{
			r = h;
		}
	}
	v2 = l;

	l = 0;
	r = N + 1;
	while(r - l > 1){
		h = (l + r) / 2;
		if(sub3(N, a, h) > M + x){
			l = h;
		}
		else{
			r = h;
		}
	}
	v3 = r;
//	printf("(v1, v2, v3) = (%d, %d, %d)\n", v1, v2, v3);

	if(max(N - v1, v3) <= v2 || v1 == N){
		return 1;
	}
	else{
		return 0;
	}
}

int main(){
	int N, M, i;
	scanf("%d%d", &N, &M);
	int *a = (int *)malloc(sizeof(int) * (2 * N));
	for(i = 0; i < 2 * N; i++){
		scanf("%d", &a[i]);
	}
	qsort(a, 2 * N, sizeof(int), compare);
	int l = -1, h, r = M;
	while(r - l > 1){
		h = (l + r) / 2;
		if(can_achieve(N, M, a, h) == 1){
//			printf("can_achieve(%d) = 1\n", h);
			r = h;
		}
		else{
//			printf("can_achieve(%d) = 0\n", h);
			l = h;
		}
	}
	printf("%d\n", r);
	return 0;
}