forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Space Optimized Solution to Subset sum problem (TheAlgorithms#5612)
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.thealgorithms.dynamicprogramming; | ||
/* | ||
The Sum of Subset problem determines whether a subset of elements from a | ||
given array sums up to a specific target value. | ||
*/ | ||
public final class SubsetSumSpaceOptimized { | ||
private SubsetSumSpaceOptimized() { | ||
} | ||
/** | ||
* This method checks whether the subset of an array | ||
* contains a given sum or not. This is an space | ||
* optimized solution using 1D boolean array | ||
* Time Complexity: O(n * sum), Space complexity: O(sum) | ||
* | ||
* @param arr An array containing integers | ||
* @param sum The target sum of the subset | ||
* @return True or False | ||
*/ | ||
public static boolean isSubsetSum(int[] arr, int sum) { | ||
int n = arr.length; | ||
// Declare the boolean array with size sum + 1 | ||
boolean[] dp = new boolean[sum + 1]; | ||
|
||
// Initialize the first element as true | ||
dp[0] = true; | ||
|
||
// Find the subset sum using 1D array | ||
for (int i = 0; i < n; i++) { | ||
for (int j = sum; j >= arr[i]; j--) { | ||
dp[j] = dp[j] || dp[j - arr[i]]; | ||
} | ||
} | ||
return dp[sum]; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimizedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.thealgorithms.dynamicprogramming; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class SubsetSumSpaceOptimizedTest { | ||
|
||
@Test | ||
void basicCheck() { | ||
assertTrue(SubsetSumSpaceOptimized.isSubsetSum(new int[] {7, 3, 2, 5, 8}, 14)); | ||
assertTrue(SubsetSumSpaceOptimized.isSubsetSum(new int[] {4, 3, 2, 1}, 5)); | ||
assertTrue(SubsetSumSpaceOptimized.isSubsetSum(new int[] {1, 7, 2, 9, 10}, 13)); | ||
assertFalse(SubsetSumSpaceOptimized.isSubsetSum(new int[] {1, 2, 7, 10, 9}, 14)); | ||
assertFalse(SubsetSumSpaceOptimized.isSubsetSum(new int[] {2, 15, 1, 6, 7}, 4)); | ||
} | ||
} |