1

You are given a list S of positive integers. You are to create two sub-lists S1 and S2 taking elements from S, such that

  1. sum of S1 is equal to sum of S2
  2. maximize this sum and output it
  3. no need to put all elements of S into S1 or S2, you can ignore any number of elements of S.

For example: if S= [2, 6, 5, 1, 2, 5] then answer will be 10, (with S1= [6, 2, 2] and S2= [5, 5] with ignoring 1)

Constraints:

(i) if N is length of S, 1<= N <=50

(ii) if MAX is the maximum element of S, 1<= MAX <=1000

(iii) if SUM is the total sum of S, 1<= SUM <=1000

I can think only below brute-force approach:

as 1<=SUM<=1000, our answer can be between 0 and 500.

so, for each subset (SUB) of S, I am finding sum (SUBSUM) of SUB and checking whether SUBSUM can be obtained from list S - SUB using subset-sum algorithm. I am returning largest such SUBSUM. So my time complexity is O(N*(SUM/2)*2^N)

Any faster way?

Any DP approach?

rd10
  • 1,599
  • 2
  • 10
  • 27
Sushil Verma
  • 659
  • 9
  • 23

0 Answers0