hello , im working on a program that tries to solve the partition problem: given n positive integers, partition them into two disjoint subsets with the same sum of
their elements. im supposed to implement a MySet class to do this and then use it to solve the problem. Ive completed the Myset class but problem is that i have no idea how do the next part. i dont understand how to use the MySet class to solve this. below are the actual instructions and what i have so far.
Your program should utilize your
MySet class to solve this problem. If no solution exists, print, “No solution exists” to the console. If a solution exists,
print the contents of each set, and the sum.
Example 1:
Enter a set of numbers: 1 2 3 4
<1,4> = <2,3> = 5
Example
Your MySet should probably have a bit more interface.
As to the problem.
If setC == setA + setB
and setA.sum() == setB.sum() == sumS
then 2 * sumS == setC.sum()
Therefore,
If setC.sum() is odd, then there is no solution
If any element is larger than setC.sum() / 2 then there is no solution
You could try to move the largest element to the other set (setB) first.
Then you still have to find elements from setA that sum up to sumS - setB.sum()