From 6f67f1f4588e80f84e0a489661f10711c148681a Mon Sep 17 00:00:00 2001 From: =sankethl27 Date: Tue, 8 Oct 2024 18:29:03 +0530 Subject: [PATCH 1/3] Optimised Space Complexity To O(sum) --- .../dynamicprogramming/SubsetSum.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java index 3dd41d2fdc0f..6d6a763098e3 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java @@ -13,24 +13,27 @@ private SubsetSum() { */ public static boolean subsetSum(int[] arr, int sum) { int n = arr.length; - boolean[][] isSum = new boolean[n + 1][sum + 1]; - // Initialize the first column to true since a sum of 0 can always be achieved with an empty subset. - for (int i = 0; i <= n; i++) { - isSum[i][0] = true; - } + //Intialize Two Arrays to store current and prev states + boolean[] isSumCurr = new boolean[sum + 1]; + boolean[] isSumPrev = new boolean[sum+1]; + + // Mark prev[0] = true as it is true to make sum = 0 + // using 0 elements + isSumPrev[0] = true; // Fill the subset sum matrix for (int i = 1; i <= n; i++) { - for (int j = 1; j <= sum; j++) { + for (int j = 0; j <= sum; j++) { if (arr[i - 1] <= j) { - isSum[i][j] = isSum[i - 1][j] || isSum[i - 1][j - arr[i - 1]]; + isSumCurr[j] = isSumPrev[j] || isSumPrev[j - arr[i - 1]]; } else { - isSum[i][j] = isSum[i - 1][j]; + isSumCurr[j] = isSumPrev[j]; } } + isSumPrev = isSumCurr.clone(); } - return isSum[n][sum]; + return isSumPrev[sum]; } } From 97e1c734815c5e4dd8765fddda8c817f2833ae12 Mon Sep 17 00:00:00 2001 From: =sankethl27 Date: Tue, 8 Oct 2024 20:13:21 +0530 Subject: [PATCH 2/3] Fixes Clang Format --- .../thealgorithms/dynamicprogramming/SubsetSum.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java index 6d6a763098e3..1abded7b3964 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java @@ -9,17 +9,17 @@ private SubsetSum() { * * @param arr the array containing integers. * @param sum the target sum of the subset. - * @return {@code true} if a subset exists that sums to the given value, otherwise {@code false}. + * @return {@code true} if a subset exists that sums to the given value, + * otherwise {@code false}. */ public static boolean subsetSum(int[] arr, int sum) { int n = arr.length; - //Intialize Two Arrays to store current and prev states + // Intialize Two Arrays to store current and prev states boolean[] isSumCurr = new boolean[sum + 1]; - boolean[] isSumPrev = new boolean[sum+1]; + boolean[] isSumPrev = new boolean[sum + 1]; - // Mark prev[0] = true as it is true to make sum = 0 - // using 0 elements + // Mark prev[0] = true as it is true to make sum = 0 using 0 elements isSumPrev[0] = true; // Fill the subset sum matrix From 64810f84a0283bf422e9df8b1594305f0ac5192c Mon Sep 17 00:00:00 2001 From: =sankethl27 Date: Wed, 9 Oct 2024 18:59:50 +0530 Subject: [PATCH 3/3] Optimised Space Complexity To Use a Single DP Array --- .../dynamicprogramming/SubsetSum.java | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java index 1abded7b3964..da8667afd0ce 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java @@ -15,25 +15,19 @@ private SubsetSum() { public static boolean subsetSum(int[] arr, int sum) { int n = arr.length; - // Intialize Two Arrays to store current and prev states - boolean[] isSumCurr = new boolean[sum + 1]; - boolean[] isSumPrev = new boolean[sum + 1]; + // Initialize a single array to store the possible sums + boolean[] isSum = new boolean[sum + 1]; - // Mark prev[0] = true as it is true to make sum = 0 using 0 elements - isSumPrev[0] = true; + // Mark isSum[0] = true since a sum of 0 is always possible with 0 elements + isSum[0] = true; - // Fill the subset sum matrix - for (int i = 1; i <= n; i++) { - for (int j = 0; j <= sum; j++) { - if (arr[i - 1] <= j) { - isSumCurr[j] = isSumPrev[j] || isSumPrev[j - arr[i - 1]]; - } else { - isSumCurr[j] = isSumPrev[j]; - } + // Iterate through each Element in the array + for (int i = 0; i < n; i++) { + // Traverse the isSum array backwards to prevent overwriting values + for (int j = sum; j >= arr[i]; j--) { + isSum[j] = isSum[j] || isSum[j - arr[i]]; } - isSumPrev = isSumCurr.clone(); } - - return isSumPrev[sum]; + return isSum[sum]; } }