710349. Partition a set into two subsets such that the difference of subset sums is minimum (Set-2)

Partition a set into two subsets such that the difference of subset sums is minimum (Set-2)

Summary

Given a set of integers, partition it into two subsets such that the absolute difference between their sums is minimized. This problem involves dynamic programming and requires finding an optimal solution by considering all possible subsets.

Detailed Explanation

The given problem can be solved using dynamic programming. We will use a 2D array dp where dp[i][j] represents whether we can partition the first i elements into two sets such that their sum difference is j. Initially, we set all values of dp to false because it's not possible to partition an empty set.

Now, for each element in the set, if the current element is less than or equal to j, we update dp[i][j] to true if either:

  • The left half (i.e., elements from 0 to i-1) can be partitioned into two sets with a sum difference of j - a[i], and
  • The right half (i.e., elements from i+1 to n-1) can be partitioned into two sets with a sum difference of a[i]

Here's the step-by-step breakdown:

  1. Initialize dp as a 2D array where dp[i][j] = false for all i, j.
  2. Iterate through each element in the set from left to right:
    • If the current element is less than or equal to j:
      • Update dp[i][j] to true if either:
        • The left half (i.e., elements from 0 to i-1) can be partitioned into two sets with a sum difference of j - a[i], and
        • The right half (i.e., elements from i+1 to n-1) can be partitioned into two sets with a sum difference of a[i]
  3. Find the minimum absolute difference between any pair of sums.

Time complexity: O(n * S), where n is the number of elements and S is the maximum value in the set. Space complexity: O(n * S).

Optimized Solutions

Java

public class Solution {
    public int minDiff(int[] a) {
        int n = a.length;
        int[] sum = new int[n + 1];
        for (int i = 0; i < n; i++) {
            sum[i + 1] = sum[i] + a[i];
        }
        
        int[][] dp = new int[n][sum[n]];
        dp[0][0] = true;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= sum[n]; j++) {
                if (a[i] <= j) {
                    dp[i][j] = dp[i - 1][j - a[i]] || dp[i - 1][j];
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        
        int minDiff = Integer.MAX_VALUE;
        for (int j = sum[n] / 2; j >= 0; j--) {
            if (dp[n - 1][sum[n] - j]) {
                minDiff = Math.min(minDiff, sum[n] - j);
                break;
            }
        }
        
        return minDiff;
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.