Dynamic programming is the solution you're looking for.
Example with [4, 3, 10, 3, 2, 5]:
X-Axis: Reachable sum of group. max = sum(all numbers) / 2 (rounded up)
Y-Axis: Count elements in group. max = count numbers / 2 (rounded up)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 | | | | 4| | | | | | | | | | | // 4
2 | | | | | | | | | | | | | | |
3 | | | | | | | | | | | | | | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 | | | 3| 4| | | | | | | | | | | // 3
2 | | | | | | | 3| | | | | | | |
3 | | | | | | | | | | | | | | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 | | | 3| 4| | | | | |10| | | | | // 10
2 | | | | | | | 3| | | | | |10|10|
3 | | | | | | | | | | | | | | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 | | | 3| 4| | | | | |10| | | | | // 3
2 | | | | | | 3| 3| | | | | |10|10|
3 | | | | | | | | | | 3| | | | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 | | 2| 3| 4| | | | | |10| | | | | // 2
2 | | | | | 2| 3| 3| | | | | 2|10|10|
3 | | | | | | | | 2| 2| 3| | | | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 | | 2| 3| 4| 5| | | | |10| | | | | // 5
2 | | | | | 2| 3| 3| 5| 5| | | 2|10|10|
3 | | | | | | | | 2| 2| 3| 5| 5| | |
^
12 is our lucky number! Backtracing to get the group:
12 - 5 = 7 {5}
7 - 3 = 4 {5, 3}
4 - 4 = 0 {5, 3, 4}
The other set can then be calculated: {4,3,10,3,2,5} - {5,3,4} = {10,3,2}
All fields with a number are possible solutions for one bag. Choose the one that is furthest in the bottom right corner.
BTW: It's called the knapsack-problem.
If all weights (w1, ..., wn and W) are
nonnegative integers, the knapsack
problem can be solved in
pseudo-polynomial time using dynamic
programming.