0

An array of students age is given : $ A = \{ A_1,A_2, ..., A_n \} $. Form maximum number of teams such that the sum of age for each team is more than 18. Note that each team consists of only 2 people.

I came across this question in an algorithm book without any answer.

My attempt :

Sort A in ascending order. Calculate the sum of biggest and smallest age from A. If the sum was more than 18 then these two should form a team, otherwise remove the smaller one from A. Continue this algorithm until we reach $|A| = 0$ or $|A| = 1$.

I believe this algorithm is the optimal answer but I don't know how to prove it.

1 Answers1

1

If you had an optimal solution not containing the oldest player, then we take any team and replace the older team member with the oldest player, and you would still have an optimal solution. So we know there is an optimal solution including the oldest player.

If oldest player and some player X together are not older than 18, then player X cannot be member of any team, so they can be removed without affecting the solution.

That done, if there is an optimal solution including the oldest player and some other player who is not the youngest player, you can replace the other player with the youngest player, and the solution is still optimal.

So you can find an optimal solution by: 1. Picking the oldest player X. 2. Removing all players Y where X + Y ≤ 18. 3. Having no team at all if X is the only player remaining. 4. Forming a team consisting of X and the youngest player, then putting the other players into teams in an optimal way.

gnasher729
  • 29,996
  • 34
  • 54