Finding the maximum sum that can be formed from a set, by partitioning it into two subset

一世执手 提交于 2021-02-17 02:55:08

问题


Decription

Given a set of numbers S.
Find maximum sum such that
Sum(A1) = Sum(A2)
Where, A1⊂S and A2⊂S and A1⋂A2=∅
And Sum(X), is the sum of all elements within the set X.

Approach

Brute Force

The easiest approach is:

print maximumSum(0,0,0)
def maximumSum(index,sum1,sum2):
  ans=0
  if sum1 == sum2:
    ans=sum1
  if index >= len(S):
    return ans
  m1=maximumSum(index+1,sum1+S[index],sum2)
  m2=maximumSum(index+1,sum1,sum2+S[index])
  m3=maximumSum(index+1,sum1,sum2)
  return max(m,m1,m2,m3)

Time Complexity:O(2N)
Space Complexity:O(1)

Is there a better approach than this?
Optional:
I would like to know whether the given problem is an NP-Complete problem or not.

Edit:

Limits

1 <= Sum(S) <= 1000000
2 <= len(S) <= 100
Time Limit: 60sec(can vary depending upon language used)


回答1:


Yes It is NPC problem Partition Problem

You can see the pseudo polynomial algorithm part if the sum of the set is small



来源:https://stackoverflow.com/questions/29247004/finding-the-maximum-sum-that-can-be-formed-from-a-set-by-partitioning-it-into-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!