Given a list of integers l
, how can I partition it into 2 lists a
and b
such that d(a,b) = abs(sum(a) - sum(b))
is minimum. I know the problem is NP-complete, so I am looking for a pseudo-polynomial time algorithm i.e. O(c*n)
where c = sum(l map abs)
. I looked at Wikipedia but the algorithm there is to partition it into exact halves which is a special case of what I am looking for...
EDIT:
To clarify, I am looking for the exact partitions a
and b
and not just the resulting minimum difference d(a, b)
To generalize, what is a pseudo-polynomial time algorithm to partition a list of n
numbers into k
groups g1, g2 ...gk
such that (max(S) - min(S)).abs
is as small as possible where S = [sum(g1), sum(g2), ... sum(gk)]