-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsetSum.java
More file actions
93 lines (81 loc) · 2.82 KB
/
subsetSum.java
File metadata and controls
93 lines (81 loc) · 2.82 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package DynamicProgramming;
import java.util.ArrayList;
public class subsetSum {
// Recursion + Memoization
public static boolean subset(int[] arr , int target , int idx , int[][] dp){
if(idx == arr.length){
if (target == 0)
return true;
else return false ;
}
if(dp[idx][target] != -1) return (dp[idx][target] == 1) ;
boolean ans = false ;
boolean skip = subset(arr, target, idx+1 , dp) ;
if(target - arr[idx] < 0) ans = skip ; // Only Valid for +ve No.
else{
boolean take = subset(arr, target - arr[idx], idx + 1, dp);
ans = skip || take;
}
dp[idx][target] = (ans) ? 1 : 0 ;
return ans;
}
public static boolean subsetRev(int[] arr, int target, int idx, int[][] dp) {
if (idx == -1) {
if (target == 0)
return true;
else
return false;
}
if (dp[idx][target] != -1)
return (dp[idx][target] == 1);
boolean ans = false;
boolean skip = subsetRev(arr, target, idx - 1, dp);
if (target - arr[idx] < 0)
ans = skip; // Only Valid for +ve No.
else {
boolean take = subsetRev(arr, target - arr[idx], idx - 1, dp);
ans = skip || take;
}
dp[idx][target] = (ans) ? 1 : 0;
return ans;
}
public static void subsetArr(int[] arr , int target , int idx , ArrayList<Integer> ans , ArrayList<ArrayList<Integer>> list ) {
if( idx == arr.length){
if(target == 0){
ArrayList<Integer> temp = new ArrayList<>() ;
for(int i = 0 ; i < ans.size() ; i++){
temp.add(ans.get(i)) ;
}
list.add(temp) ;
}
return ;
}
// skip
subsetArr(arr, target, idx+1, ans , list) ;
// pick
// if( target - arr[idx] < 0){
// return ;
// }
// else{
ans.add(arr[idx]) ;
subsetArr(arr, target-arr[idx], idx+1, ans , list ) ;
ans.remove(ans.size()-1) ;
// }
}
public static void main(String[] args) {
int[] arr = { 8 , 1 , 2 , 4 , 5 , -2 , -3} ;
int target = 6 ;
// i = 0 to n-1 | target = target to 0
int[][] dp = new int[arr.length][target+1] ;
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
dp[i][j] = -1 ;
}
}
ArrayList<ArrayList<Integer>> list = new ArrayList<>() ;
subsetArr(arr, target, 0, new ArrayList<>() , list);
System.out.println(list);
// System.out.println(subset(arr, target, 0 , dp)) ;
// Tabulation
}
}