#K66717. Pair Participants to Minimize Score Differences
Pair Participants to Minimize Score Differences
Pair Participants to Minimize Score Differences
Given an even number of participants and a list of their scores, your task is to form pairs such that the sum of score differences is minimized.
The optimal strategy is to first sort the scores in ascending order and then pair adjacent elements. In mathematical terms, if the sorted scores are given as $$s_1, s_2, \dots, s_n$$, the objective is to minimize $$\sum_{i=1}^{\frac{n}{2}} (s_{2i} - s_{2i-1})$$.
inputFormat
Input is given via standard input (stdin). The first line contains an even integer (n) representing the number of participants. The second line contains (n) space-separated integers representing the scores of the participants.
outputFormat
Output the resulting pairs, each on a separate line. Each line should contain two space-separated integers representing a pair's scores in increasing order. The pairs must appear in the order derived from the sorted input.## sample
4
3 1 2 4
1 2
3 4
</p>