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 integers . It is known that is even. Petya must divide the participants (numbers) into exactly pairs . Each index can be included in no more than one pair.
For a pair , its difference is defined as . 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 in non-decreasing order. Let the sorted array be .
The optimal pairing strategy is to form pairs . The difference for each pair is for .
The maximum difference among these pairs will be .
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 with and with where , the differences are and . The alternative pairing and would result in smaller differences, and .
Therefore, the minimum possible value of the maximum difference is the maximum difference between adjacent elements in the sorted array, taken in pairs.