B. Unconventional Pairs

Problem Statement

A popular reality show Unconventional Pairs has been launched in the city. According to the rules of the show, participants are paired in an unusual way: with an even number of people, all participants must be in pairs.

Petya has an array of nn integers a1,a2,,ana_1, a_2, \ldots, a_n. It is known that nn is even. Petya must divide the participants (numbers) into exactly n2\frac{n}{2} pairs (ap1,aq1),(ap2,aq2),,(apn2,aqn2)(a_{p_1}, a_{q_1}), (a_{p_2}, a_{q_2}), \ldots, (a_{p_{\frac{n}{2}}}, a_{q_{\frac{n}{2}}}). Each index can be included in no more than one pair.

For a pair (x,y)(x, y), its difference is defined as xy|x - y|. Petya wants to form unconventional pairs such that the maximum difference among all pairs is minimized.

Determine the minimum possible value of this maximum difference.

Solution

To minimize the maximum difference among all pairs, the best strategy is to pair adjacent elements after sorting the array.

First, sort the array aa in non-decreasing order. Let the sorted array be a1,a2,,ana'_1, a'_2, \ldots, a'_n.

The optimal pairing strategy is to form pairs (a1,a2),(a3,a4),,(an1,an)(a'_1, a'_2), (a'_3, a'_4), \ldots, (a'_{n-1}, a'_n). The difference for each pair (a2i1,a2i)(a'_{2i-1}, a'_{2i}) is a2ia2i1a'_{2i} - a'_{2i-1} for i=1,,n2i = 1, \ldots, \frac{n}{2}.

The maximum difference among these pairs will be maxi=1n2(a2ia2i1)\max_{i=1}^{\frac{n}{2}} (a'_{2i} - a'_{2i-1}).

This strategy is optimal because any other pairing would involve “crossing over” elements, which would lead to a larger or equal maximum difference. For example, if we pair aia'_i with aka'_k and aja'_j with ala'_l where i<j<k<li < j < k < l, the differences are akaia'_k - a'_i and alaja'_l - a'_j. The alternative pairing (ai,aj)(a'_i, a'_j) and (ak,al)(a'_k, a'_l) would result in smaller differences, ajaia'_j - a'_i and alaka'_l - a'_k.

Therefore, the minimum possible value of the maximum difference is the maximum difference between adjacent elements in the sorted array, taken in pairs.

Submission Link