-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtargetSumSubset.java
47 lines (40 loc) · 1.47 KB
/
targetSumSubset.java
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
/*1. You are given a number n, representing the count of elements.
2. You are given n numbers.
3. You are given a number "tar".
4. You are required to calculate and print true or false, if there is a subset the elements of which add
up to "tar" or not. */
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = sc.nextInt();
}
int tar = sc.nextInt();
boolean[][] dp = new boolean[arr.length + 1][tar + 1];
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
if (i == 0 && j == 0) {
dp[i][j] = true;
} else if (i == 0) {
dp[i][j] = false;
} else if (j == 0) {
dp[i][j] = true;
} else {
if(dp[i - 1][j] == true){
dp[i][j] = true;
} else {
int val = arr[i - 1];
if (j >= val && dp[i - 1][j - val] == true) {
dp[i][j] = true;
}
}
}
}
}
System.out.println(dp[dp.length - 1][tar]);
}
}