# How to find best combinations of numbers to make the sums as close as possible.

Say I am given eight numbers: 10, 8, 8, 7, 6. 5, 5, 4

I am told to divide the group of numbers in to two different groups, four numbers each. What formula or method is there for running every possible combination of numbers possible, and in the end output two groups who's numbers' sum is even or as close as possible.

I am looking for an algorithm for an Android application I am developing. The user inputs the names and skill of each player playing in a game of the users choice. The application will read the inputs and list the best combination of players so that the teams are as fair as possible for the given skill levels. Please let me know if you have any questions or need any more information. Thank you.

-
Is there a limit to the number of players? What about the skill rating values? – Aryabhata Apr 30 '12 at 21:45
Range of number of players: 4 to 8, Range of skill: 0 to 10. – Jonah Apr 30 '12 at 21:46
If I am not mistaken this is related to the knapsack problem. en.wikipedia.org/wiki/Knapsack_problem and thus NP-complete. A lot of algorithms give decent results though and with a small enough set (8players?) you could even try every reasonable possibility by brute force. – example Apr 30 '12 at 21:51
Thank you for the link to the Knapsack Problem, I have never heard of that. Do you have any idea how I could come up with a set formula for this? Since this will be applied to a computer program, I cannot sit and manually work each scenario out on paper. – Jonah Apr 30 '12 at 21:59
Also lookup subset sum problem. – Aryabhata Apr 30 '12 at 22:51

If you want to run all possible combinations, there are ${n \choose n/2}=\frac {n!}{((n/2)!)^2}$ of them. Some bounds are given in Wikipedia. If you generate the combinations, one approach is given here. You start with the first n/2 as the first combination, then move the last element one later until you can't any more, move the next to last one later and the last as early as possible, and so on.
You could, without loss of generality, assign some player to team 1. You only need to look at the number of ways to complete team 1. For example, with 8 players, you would need to check $\binom{7}{3} = 35$ collections of teammates for the fixed player on team 1. This is very reasonable computationally. – Austin Mohr Apr 30 '12 at 22:04