-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsetSum.java
More file actions
66 lines (51 loc) · 1.59 KB
/
Copy pathSubsetSum.java
File metadata and controls
66 lines (51 loc) · 1.59 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
58
59
60
61
62
63
64
65
66
#Problem [https://practice.geeksforgeeks.org/problems/subset-sum-problem-1611555638/1/#]
#Solution:
// { Driver Code Starts
import java.io.*;
import java.util.*;
class GFG
{
public static void main(String args[])throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
int N = Integer.parseInt(read.readLine());
String input_line[] = read.readLine().trim().split("\\s+");
int arr[]= new int[N];
for(int i = 0; i < N; i++)
arr[i] = Integer.parseInt(input_line[i]);
int sum = Integer.parseInt(read.readLine());
Solution ob = new Solution();
if(ob.isSubsetSum(N, arr, sum))
System.out.println(1);
else
System.out.println(0);
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution{
static Boolean isSubsetSum(int N, int arr[], int sum){
Boolean t[][] = new Boolean[N+1][sum+1];
for(int i=0;i<sum+1;i++){
t[0][i]= false;
}
for(int i=0;i<N+1;i++){
t[i][0]= true;
}
for(int i=1;i<N+1;i++){
for(int j=1;j<sum+1;j++){
if(arr[i-1]<=j){
t[i][j] = t[i-1][j-arr[i-1]]||t[i-1][j];
}else{
t[i][j]=t[i-1][j];
}
}
}
return t[N][sum];
}
}
#Problem of type 0/1 Knapsack