Skip to content

Commit 1e4d747

Browse files
committed
Fix: Dynamic Programming - Count of subset sums problems
1 parent 5ce5cc4 commit 1e4d747

File tree

2 files changed

+3
-6
lines changed

2 files changed

+3
-6
lines changed

DynamicProgramming/CountOfSubsetSumsEqualToGivenSum/Solution.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ private int countOfSubsetsDP(int[] array, int sum) {
2525

2626
for (int i = 0; i < len + 1; i++) dp[i][0] = 1;
2727

28-
for (int j = 1; j < sum + 1; j++) dp[0][j] = 0;
29-
3028
for (int i = 1; i < len + 1; i++) {
31-
for (int j = 1; j < sum + 1; j++) {
29+
for (int j = 0; j < sum + 1; j++) {
3230
// If the current item is less than
3331
// or equal to the current sum, then we
3432
// need to consider both the scenarios

DynamicProgramming/CountOfSubsetsWithAGivenDifference/Solution.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ private int countOfSubsetsDP(int[] array, int sum) {
4343

4444
for (int i = 0; i < len + 1; i++) dp[i][0] = 1;
4545

46-
for (int j = 1; j < sum + 1; j++) dp[0][j] = 0;
47-
4846
for (int i = 1; i < len + 1; i++) {
49-
for (int j = 1; j < sum + 1; j++) {
47+
for (int j = 0; j < sum + 1; j++) {
5048
// If the current item is less than
5149
// or equal to the current sum, then we
5250
// need to consider both the scenarios
@@ -68,6 +66,7 @@ private int countOfSubsetsDP(int[] array, int sum) {
6866
public static void main(String[] args) {
6967
Solution solution = new Solution();
7068

69+
// should be 3
7170
System.out.println(solution.countOfSubsets(new int[] {1, 1, 2, 3}, 1));
7271
}
7372
}

0 commit comments

Comments
 (0)