First of all, you are right that iteratively combining the best remaining student with the worst remaining student gives you the optimal result (see the proof below).
However, you did not calculate the cost of this solution correctly. You must run all the pairs of your combination to find the quality value of the best and worst pairs, and only after you find them, you can subtract their values. Btw. you do not need a priority queue to find the minimum (they are considered for more complex use cases, when you insert and set several times and even update the values in the queue), I will do simple battery variables ( min and max ). Assuming array a already sorted, the code might look like this:
int min = 2 * MAXIMUM_KNOWLEDGE_LEVEL; int max = 2 * MINIMUM_KNOWLEDGE_LEVEL; for (int i = 0; i < num / 2; i++) { int quality = a[i]+a[num-1-i]; if (quality < min) { min = quality; } if (quality > max) { max = quality; } } int result = max - min;
Now I want to prove why the pairing you have chosen (iteratively, pairs of the best remaining students with the worst remaining student) is optimal, which is significant here. If this fails, we need a whole different algorithm.
We prove this by showing that it is not possible to improve the solution given by this pairing.
If we wanted to improve it, we would have to reduce the difference between the best and the worst pair, which means either a decrease in the best pair or an increase in the worst pair (or both).
Let them first show why lowering the best pair is impossible.
Let pair indices be given: the pair containing the largest number and the lowest number will be p_1 = (a_1, b_1), the pair with the next highest number and the next lowest number will be p_2 = (a_2, b_2), and so on, up to p_n. For instance:
pab quality(p) p_1 = (a_1, b_1) = (10, 1) -> 11 (= min) p_2 = (a_2, b_2) = ( 9, 4) -> 13 p_3 = (a_3, b_3) = ( 8, 5) -> 13 p_4 = (a_4, b_4) = ( 8, 7) -> 15 (= max) p_5 = (a_5, b_5) = ( 7, 7) -> 14
Now one of these pairs, let it be called p_m = (a_m, b_m) (with 1 <= m <= n) will be the maximum pair. If we want to lower the maximum, we will have to break this pair. But with whom can we pair a_m , so that the new pair has a lower amount? We need to find a b , let's call it b_y below b_m (otherwise this would not be an improvement). We can only find the bottom b_y by going up the list, i.e. y < m . But the same thing happens for all pairs: if we have a pair up ( p_x with x < m ) and try to find a new partner b_y for the old a_x , we must take into account that a_x >= a_m , which makes it impossible to choose from y . If we choose a y>=m , this means that b_y >= b_m and, therefore, quality(a_x, b_y) = a_x + b_y >= a_m + b_y >= a_m + b_m = quality(p_m) , which we wanted to avoid . So y should be below m . Given this limitation, m possible values for a ( {a_1,...,a_m} ) have only m-1 possible partners {b_1,...b_(m-1)} , so pairing is not possible. Therefore, a decrease in the value of the best pair was impossible.
In the above example: To reduce the maximum of 15, all pairs must have values lower than 15. This means that all left values of the pairs are higher or equal to the maximum pair (8, 8, 9, 10) partners will be required that are lower than the maximum partner on the right side (7); therefore, only 1, 4, and 5 are possible.

The proof that increasing the worst pair is impossible works the same with the inverse comparison operators and switches to a and b (in this case, too much b for too few a s).