一堆数分成两份,求这两份的差值最小
如题所述,给出n个正整数,求将这n个数分成两份,一份有m(m<n)个数,那么另一份就有n-m个数。然后每一份求和,要求得到的差值的绝对值最小。谢谢。
[解决办法]
假设数组为A[1..n]
首先计算数组元素的和s;
那么这个题就可以对应到0-1背包问题:
有n个物品,每个物品的重量为A[i],其价值为A[i];
背包的容量为s/2;
[解决办法]
This is not NP problem. Could be solve in O(N*Sum)
Ref: http://community.csdn.net/expert/topicView.asp?id=3655455
In the refrence, it is to find two subarray with same sum. The code for this problem is very similar