mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
* Enhance Minimum sum partition problem implementation * Linter resolved * Linter resolved * Code review comments * Code review comments * Add validation for non-negative numbers * Linter resolved * style: fix formiatting --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -1,89 +1,57 @@
|
|||||||
package com.thealgorithms.dynamicprogramming;
|
package com.thealgorithms.dynamicprogramming;
|
||||||
|
|
||||||
// Partition a set into two subsets such that the difference of subset sums is minimum
|
import java.util.Arrays;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Input: arr[] = {1, 6, 11, 5}
|
Given an array of non-negative integers , partition the array in two subset that
|
||||||
Output: 1
|
difference in sum of elements for both subset minimum.
|
||||||
|
Return the minimum difference in sum of these subsets you can achieve.
|
||||||
|
|
||||||
|
Input: array[] = {1, 6, 11, 4}
|
||||||
|
Output: 0
|
||||||
Explanation:
|
Explanation:
|
||||||
Subset1 = {1, 5, 6}, sum of Subset1 = 12
|
Subset1 = {1, 4, 6}, sum of Subset1 = 11
|
||||||
Subset2 = {11}, sum of Subset2 = 11
|
Subset2 = {11}, sum of Subset2 = 11
|
||||||
|
|
||||||
Input: arr[] = {36, 7, 46, 40}
|
Input: array[] = {36, 7, 46, 40}
|
||||||
Output: 23
|
Output: 23
|
||||||
Explanation:
|
Explanation:
|
||||||
Subset1 = {7, 46} ; sum of Subset1 = 53
|
Subset1 = {7, 46} ; sum of Subset1 = 53
|
||||||
Subset2 = {36, 40} ; sum of Subset2 = 76
|
Subset2 = {36, 40} ; sum of Subset2 = 76
|
||||||
*/
|
*/
|
||||||
public class MinimumSumPartition {
|
public final class MinimumSumPartition {
|
||||||
|
private MinimumSumPartition() {
|
||||||
|
}
|
||||||
|
|
||||||
public static int subSet(int[] arr) {
|
private static void throwIfInvalidInput(final int[] array) {
|
||||||
int n = arr.length;
|
if (Arrays.stream(array).anyMatch(a -> a < 0)) {
|
||||||
int sum = getSum(arr);
|
throw new IllegalArgumentException("Input array should not contain negative number(s).");
|
||||||
boolean[][] dp = new boolean[n + 1][sum + 1];
|
|
||||||
for (int i = 0; i <= n; i++) {
|
|
||||||
dp[i][0] = true;
|
|
||||||
}
|
|
||||||
for (int j = 0; j <= sum; j++) {
|
|
||||||
dp[0][j] = false;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// fill dp array
|
public static int minimumSumPartition(final int[] array) {
|
||||||
for (int i = 1; i <= n; i++) {
|
throwIfInvalidInput(array);
|
||||||
for (int j = 1; j <= sum; j++) {
|
int sum = Arrays.stream(array).sum();
|
||||||
if (arr[i - 1] < j) {
|
boolean[] dp = new boolean[sum / 2 + 1];
|
||||||
dp[i][j] = dp[i - 1][j - arr[i - 1]] || dp[i - 1][j];
|
dp[0] = true; // Base case , don't select any element from array
|
||||||
} else if (arr[i - 1] == j) {
|
|
||||||
dp[i][j] = true;
|
// Find the closest sum of subset array that we can achieve which is closest to half of sum of full array
|
||||||
} else {
|
int closestPartitionSum = 0;
|
||||||
dp[i][j] = dp[i - 1][j];
|
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
for (int j = sum / 2; j > 0; j--) {
|
||||||
|
if (array[i] <= j) {
|
||||||
|
dp[j] = dp[j] || dp[j - array[i]];
|
||||||
|
}
|
||||||
|
if (dp[j]) {
|
||||||
|
closestPartitionSum = Math.max(closestPartitionSum, j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
// fill the index array
|
Difference in sum = Big partition sum - Small partition sum
|
||||||
int[] index = new int[sum];
|
= ( Total sum - Small partition sum) - Small partition sum
|
||||||
int p = 0;
|
*/
|
||||||
for (int i = 0; i <= sum / 2; i++) {
|
return sum - (2 * closestPartitionSum);
|
||||||
if (dp[n][i]) {
|
|
||||||
index[p++] = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return getMin(index, sum);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate sum of array elements
|
|
||||||
*
|
|
||||||
* @param arr the array
|
|
||||||
* @return sum of given array
|
|
||||||
*/
|
|
||||||
public static int getSum(int[] arr) {
|
|
||||||
int sum = 0;
|
|
||||||
for (int temp : arr) {
|
|
||||||
sum += temp;
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getMin(int[] arr, int sum) {
|
|
||||||
if (arr.length == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int min = Integer.MAX_VALUE;
|
|
||||||
for (int temp : arr) {
|
|
||||||
min = Math.min(min, sum - 2 * temp);
|
|
||||||
}
|
|
||||||
return min;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Driver Code
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
assert subSet(new int[] {1, 6, 11, 5}) == 1;
|
|
||||||
assert subSet(new int[] {36, 7, 46, 40}) == 23;
|
|
||||||
assert subSet(new int[] {1, 2, 3, 9}) == 3;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.thealgorithms.dynamicprogramming;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class MinimumSumPartitionTest {
|
||||||
|
@Test
|
||||||
|
public void testMinimumSumPartitionWithEvenSum() {
|
||||||
|
int[] array = {1, 6, 11, 4};
|
||||||
|
assertEquals(0, MinimumSumPartition.minimumSumPartition(array));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMinimumSumPartitionWithOddSum() {
|
||||||
|
int[] array = {36, 7, 46, 40};
|
||||||
|
assertEquals(23, MinimumSumPartition.minimumSumPartition(array));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMinimumSumPartitionWithSingleElement() {
|
||||||
|
int[] array = {7};
|
||||||
|
assertEquals(7, MinimumSumPartition.minimumSumPartition(array));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMinimumSumPartitionWithLargeNumbers() {
|
||||||
|
int[] array = {100, 200, 300, 400, 500};
|
||||||
|
assertEquals(100, MinimumSumPartition.minimumSumPartition(array));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMinimumSumPartitionWithEmptyArray() {
|
||||||
|
int[] array = {};
|
||||||
|
assertEquals(0, MinimumSumPartition.minimumSumPartition(array));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMinimumSumPartitionThrowsForNegativeArray() {
|
||||||
|
int[] array = {4, 1, -6, 7};
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> { MinimumSumPartition.minimumSumPartition(array); });
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user