This is a hard algorithms problem that :
Divide the list in 2 parts (sum) that their sum closest to (most) each other
list length is 1 <= n <= 100 and their(numbers) weights 1<=w<=250 given in the question.
For example : 23 65 134 32 95 123 34
1.sum = 256
2.sum = 250
1.list = 1 2 3 7
2.list = 4 5 6
I have an algorithm but it didn't work for all inputs.
- init. lists list1 = [], list2 = []
- Sort elements (given list) [23 32 34 65 95 123 134]
- pop last one (max one)
- insert to the list which differs less
Implementation :
list1 = [], list2 = []
- select 134 insert list1. list1 = [134]
- select 123 insert list2. because if you insert to the list1 the difference getting bigger
3. select 95 and insert list2 . because sum(list2) + 95 - sum(list1) is less.
and so on...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…