|
1 | 1 | package com.thealgorithms.dynamicprogramming;
|
2 | 2 |
|
3 |
| -// Partition a set into two subsets such that the difference of subset sums is minimum |
| 3 | +import java.util.Arrays; |
4 | 4 |
|
5 | 5 | /*
|
6 |
| -Input: arr[] = {1, 6, 11, 5} |
7 |
| -Output: 1 |
| 6 | +Given an array of non-negative integers , partition the array in two subset that |
| 7 | +difference in sum of elements for both subset minimum. |
| 8 | +Return the minimum difference in sum of these subsets you can achieve. |
| 9 | +
|
| 10 | +Input: array[] = {1, 6, 11, 4} |
| 11 | +Output: 0 |
8 | 12 | Explanation:
|
9 |
| -Subset1 = {1, 5, 6}, sum of Subset1 = 12 |
| 13 | +Subset1 = {1, 4, 6}, sum of Subset1 = 11 |
10 | 14 | Subset2 = {11}, sum of Subset2 = 11
|
11 | 15 |
|
12 |
| -Input: arr[] = {36, 7, 46, 40} |
| 16 | +Input: array[] = {36, 7, 46, 40} |
13 | 17 | Output: 23
|
14 | 18 | Explanation:
|
15 | 19 | Subset1 = {7, 46} ; sum of Subset1 = 53
|
16 | 20 | Subset2 = {36, 40} ; sum of Subset2 = 76
|
17 | 21 | */
|
18 |
| -public class MinimumSumPartition { |
19 |
| - |
20 |
| - public static int subSet(int[] arr) { |
21 |
| - int n = arr.length; |
22 |
| - int sum = getSum(arr); |
23 |
| - boolean[][] dp = new boolean[n + 1][sum + 1]; |
24 |
| - for (int i = 0; i <= n; i++) { |
25 |
| - dp[i][0] = true; |
26 |
| - } |
27 |
| - for (int j = 0; j <= sum; j++) { |
28 |
| - dp[0][j] = false; |
29 |
| - } |
30 |
| - |
31 |
| - // fill dp array |
32 |
| - for (int i = 1; i <= n; i++) { |
33 |
| - for (int j = 1; j <= sum; j++) { |
34 |
| - if (arr[i - 1] < j) { |
35 |
| - dp[i][j] = dp[i - 1][j - arr[i - 1]] || dp[i - 1][j]; |
36 |
| - } else if (arr[i - 1] == j) { |
37 |
| - dp[i][j] = true; |
38 |
| - } else { |
39 |
| - dp[i][j] = dp[i - 1][j]; |
40 |
| - } |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - // fill the index array |
45 |
| - int[] index = new int[sum]; |
46 |
| - int p = 0; |
47 |
| - for (int i = 0; i <= sum / 2; i++) { |
48 |
| - if (dp[n][i]) { |
49 |
| - index[p++] = i; |
50 |
| - } |
51 |
| - } |
52 |
| - |
53 |
| - return getMin(index, sum); |
| 22 | +public final class MinimumSumPartition { |
| 23 | + private MinimumSumPartition() { |
54 | 24 | }
|
55 | 25 |
|
56 |
| - /** |
57 |
| - * Calculate sum of array elements |
58 |
| - * |
59 |
| - * @param arr the array |
60 |
| - * @return sum of given array |
61 |
| - */ |
62 |
| - public static int getSum(int[] arr) { |
63 |
| - int sum = 0; |
64 |
| - for (int temp : arr) { |
65 |
| - sum += temp; |
| 26 | + private static void throwIfInvalidInput(final int[] array) { |
| 27 | + if (Arrays.stream(array).anyMatch(a -> a < 0)) { |
| 28 | + throw new IllegalArgumentException("Input array should not contain negative number(s)."); |
66 | 29 | }
|
67 |
| - return sum; |
68 | 30 | }
|
69 | 31 |
|
70 |
| - public static int getMin(int[] arr, int sum) { |
71 |
| - if (arr.length == 0) { |
72 |
| - return 0; |
73 |
| - } |
74 |
| - int min = Integer.MAX_VALUE; |
75 |
| - for (int temp : arr) { |
76 |
| - min = Math.min(min, sum - 2 * temp); |
77 |
| - } |
78 |
| - return min; |
79 |
| - } |
| 32 | + public static int minimumSumPartition(final int[] array) { |
| 33 | + throwIfInvalidInput(array); |
| 34 | + int sum = Arrays.stream(array).sum(); |
| 35 | + boolean[] dp = new boolean[sum / 2 + 1]; |
| 36 | + dp[0] = true; // Base case , don't select any element from array |
80 | 37 |
|
81 |
| - /** |
82 |
| - * Driver Code |
83 |
| - */ |
84 |
| - public static void main(String[] args) { |
85 |
| - assert subSet(new int[] {1, 6, 11, 5}) == 1; |
86 |
| - assert subSet(new int[] {36, 7, 46, 40}) == 23; |
87 |
| - assert subSet(new int[] {1, 2, 3, 9}) == 3; |
| 38 | + // Find the closest sum of subset array that we can achieve which is closest to half of sum of full array |
| 39 | + int closestPartitionSum = 0; |
| 40 | + |
| 41 | + for (int i = 0; i < array.length; i++) { |
| 42 | + for (int j = sum / 2; j > 0; j--) { |
| 43 | + if (array[i] <= j) { |
| 44 | + dp[j] = dp[j] || dp[j - array[i]]; |
| 45 | + } |
| 46 | + if (dp[j]) { |
| 47 | + closestPartitionSum = Math.max(closestPartitionSum, j); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + /* |
| 52 | + Difference in sum = Big partition sum - Small partition sum |
| 53 | + = ( Total sum - Small partition sum) - Small partition sum |
| 54 | + */ |
| 55 | + return sum - (2 * closestPartitionSum); |
88 | 56 | }
|
89 | 57 | }
|
0 commit comments