Repository: TheAlgorithms/Java
Pull request #5612 (2024-10-06 09:32:59)

Added Space Optimized Solution to Subset sum problem 
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

Solved #5600 

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5612?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `0%` with `7 lines` in your changes missing coverage. Please review.
> Project coverage is 54.72%. Comparing base [(`b190cb7`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b190cb72def8d926e2f73d9f80cfd27c88ff3a3a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`c61d2ea`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/c61d2ea0d082229deb919b321203170617e03147?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5612?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...om/thealgorithms/dynamicprogramming/SubsetSum.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5612?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdynamicprogramming%2FSubsetSum.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9keW5hbWljcHJvZ3JhbW1pbmcvU3Vic2V0U3VtLmphdmE=) | 0.00% | [7 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5612      +/-   ##
============================================
- Coverage     54.75%   54.72%   -0.03%     
- Complexity     3453     3454       +1     
============================================
  Files           538      538              
  Lines         15583    15590       +7     
  Branches       2961     2964       +3     
============================================
  Hits           8532     8532              
- Misses         6694     6701       +7     
  Partials        357      357              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5612?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

Rupa-Rd: > There are no files changed in this PR

Kindly verify it!
Rupa-Rd: > Please add JUnit tests

@siriak Added JUnit test.

Patch:
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java
new file mode 100644
index 00000000..946da46c
--- /dev/null
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java
@@ -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];
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimizedTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimizedTest.java
new file mode 100644
index 00000000..3a965f4e
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimizedTest.java
@@ -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));
+    }
+}