Repository navigation
Expand file tree
/
Copy pathSubsetSum.java
More file actions
57 lines (49 loc) · 1.69 KB
/
Copy pathSubsetSum.java
File metadata and controls
57 lines (49 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package DynamicProgramming;
public class SubsetSum {
/*
This algorithm will determine if a set of integers contains
a subset that sum to a given integer. The algorithm will
return true if such a subset exists and false otherwise.
This is a dynamic programming implementation.
*/
private static boolean subsetSum(int arr[], int n, int sum){
boolean isSum[][] = new boolean[n + 2][sum + 1];
isSum[n + 1][0] = true;
for (int i = 1; i <= sum; i++) {
isSum[n + 1][i] = false;
}
for (int i = n; i > 0; i--) {
isSum[i][0] = true;
for (int j = 1; j <= arr[i - 1] - 1; j++) {
if (j <= sum) {
isSum[i][j] = isSum[i + 1][j];
}
}
for (int j = arr[i - 1]; j <= sum; j++) {
isSum[i][j] = (isSum[i + 1][j] || isSum[i + 1][j - arr[i - 1]]);
}
}
return isSum[1][sum];
}
/*
This is a driver method to run the algorithm with four
test values: the first two should evaluate true, and the
last two should evaluate false.
*/
public static void main(String args[]) {
int arr[] = new int[]{50, 4, 10, 15, 34};
int n = arr.length;
// 4 + 10 + 15 + 34 = 64
int sum1 = 64;
// 50 + 15 + 34 = 99
int sum2 = 99;
// No subset of the given array will give a sum
// of 5 or 66
int sum3 = 5;
int sum4 = 66;
System.out.println(subsetSum(arr, n, sum1));
System.out.println(subsetSum(arr, n, sum2));
System.out.println(subsetSum(arr, n, sum3));
System.out.println(subsetSum(arr, n, sum4));
}
}