...

/

Minimum Subset Sum Difference (hard)

Minimum Subset Sum Difference (hard)

Problem Statement

Given a set of positive numbers, partition the set into two subsets with minimum difference between their subset sums.

Example 1:

Input: {1, 2, 3, 9}
Output: 3
Explanation: We can partition the given set into two subsets where minimum absolute difference 
between the sum of numbers is '3'. Following are the two subsets: {1, 2, 3} & {9}.

Example 2:

Input: {1, 2, 7, 1, 5}
Output: 0
Explanation: We can partition the given set into two subsets where minimum absolute difference 
between the sum of number is '0'. Following are the two subsets: {1, 2, 5} & {7, 1}.

Example 3:

Input: {1, 3, 100, 4}
Output: 92
Explanation: We can partition the given set into two subsets where minimum absolute difference 
between the sum of numbers is '92'. Here are the two subsets: {1, 3, 4} & {100}.

Try it yourself

Try solving this question here:

class PartitionSet {
public int canPartition(int[] num) {
// TODO: Write your code here
return -1;
}
public static void main(String[] args) {
PartitionSet ps = new PartitionSet();
int[] num = {1, 2, 3, 9};
System.out.println(ps.canPartition(num));
num = new int[]{1, 2, 7, 1, 5};
System.out.println(ps.canPartition(num));
num = new int[]{1, 3, 100, 4};
System.out.println(ps.canPartition(num));
}
}

Basic Solution

This problem follows the 0/1 Knapsack pattern and can be converted into a Subset Sum problem.

Let’s assume S1 and S2 are the two desired subsets. A basic brute-force solution could be to try adding each element either in S1 or S2 in order to find the combination that gives the minimum sum difference ...