Given a set of positive integers S, partition set S into two subsets, S1 and S2, such that the difference between the sum of elements in S1 and S2 is minimized. The solution should return the minimum absolute difference between the sum of elements of two partitions.

For example, consider S = {10, 20, 15, 5, 25}.

 
We can partition S into two partitions where the minimum absolute difference between the sum of elements is 5.

S1 = {10, 20, 5}
S2 = {15, 25}

Note that this solution is not unique. The following is another solution:

S1 = {10, 25}
S2 = {20, 15, 5}

Practice this problem

This problem is an optimization version of the partition problem. The idea is to consider each item in the given set S one by one, and for each item, there are two possibilities:

  1. Include the current item in subset S1 and recur for the remaining items.
  2. Include the current item from the subset S2 and recur for the remaining items.

Finally, return the minimum difference we get by including the current item in S1 and S2. When there are no items left in the set, return the absolute difference between elements of S1 and S2.

 
Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

The minimum difference is 5

Java


Download  Run Code

Output:

The minimum difference is 5

Python


Download  Run Code

Output:

The minimum difference is 5

The time complexity of the above solution is exponential and occupies space in the call stack.

 
The problem has optimal substructure. That means the problem can be broken down into smaller, simple “subproblems”, which can further be divided into yet simpler, smaller subproblems until the solution becomes trivial. The above solution also exhibits overlapping subproblems. If we draw the solution’s recursion tree, we can see that the same subproblems are getting computed repeatedly.

We know that problems with optimal substructure and overlapping subproblems can be solved using dynamic programming, in which subproblem solutions are memoized rather than computed again and again.

Following is the memoized version in C++, Java, and Python that follows the top-down approach since we first break the problem into subproblems and then calculate and store values.

C++


Download  Run Code

Output:

The minimum difference is 5

Java


Download  Run Code

Output:

The minimum difference is 5

Python


Download  Run Code

Output:

The minimum difference is 5

The time complexity of the above top-down solution is O(n × sum) and requires O(n × sum) extra space, where n is the size of the input and sum is the sum of all elements in the input.

 
We can also implement the bottom-up version of the memoized solution. The following code shows how to implement it in C++, Java, and Python:

C++


Download  Run Code

Output:

The minimum difference is 5

Java


Download  Run Code

Output:

The minimum difference is 5

Python


Download  Run Code

Output:

The minimum difference is 5

The time complexity of the above bottom-up solution is O(n × sum) and requires O(n × sum) extra space, where n is the size of the input and sum is the sum of all elements in the input.