PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CT0000076. Cặp số gần K nhất (Pair Sum Closest to K)

Bài tập này chưa được xếp vào lộ trình.

Cặp số gần K nhất (Pair Sum Closest to K)

Pair Sum Closest to K

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn integers a1,a2,…,ana_1,a_2,\ldots,a_n and an integer KK.

Choose two distinct positions i,ji,j (1≤i<j≤n1\le i<j\le n). The absolute difference between their sum and KK is

∣ai+aj−K∣.|a_i+a_j-K|.

Find the minimum absolute difference over all valid pairs. Equal values may be selected if they occur at distinct positions. Print the minimum difference, not the indices.

Input

Line 1 contains integers nn, KK.

Line 2 contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

Output one integer: the minimum absolute difference.

Subtasks

  • Subtask 1 (30 points): 2≤n≤3002 \le n \le 300; −2⋅109≤K≤2⋅109-2\cdot10^9 \le K \le 2\cdot10^9; −109≤ai≤109-10^9 \le a_i \le 10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 2≤n≤50002 \le n \le 5000; −2⋅109≤K≤2⋅109-2\cdot10^9 \le K \le 2\cdot10^9; −109≤ai≤109-10^9 \le a_i \le 10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (40 points): 2≤n≤2⋅1052 \le n \le 2\cdot10^5; −2⋅109≤K≤2⋅109-2\cdot10^9 \le K \le 2\cdot10^9; −109≤ai≤109-10^9 \le a_i \le 10^9 for every 1≤i≤n1\le i\le n.

Examples

Example 1

Input

5 10
1 4 7 13 20

Output

1

Explanation: Values 4 and 7 occur at different positions, giving sum 11 and difference |11 − 10| = 1; no pair sums to 10.

Example 2

Input

4 -2
-7 -1 3 10

Output

2

Explanation: The pair −7 and 3 sums to −4, so the difference is |−4 − (−2)| = 2; no other pair is closer.

Example 3

Input

2 4
2 2

Output

0

Explanation: The two values of 2 occur at distinct indices. Their sum is exactly K = 4, so the difference is 0.