-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarget_Sum.java
More file actions
67 lines (50 loc) · 1.66 KB
/
Copy pathTarget_Sum.java
File metadata and controls
67 lines (50 loc) · 1.66 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
class Target_Sum{
public static int findTargetSumWays(int[] nums, int target) {
int zeros= 0;
int sum=0;
for(int i: nums){
sum += i;
if(i== 0) zeros++;
}
if(sum < Math.abs(target) || (sum + target) % 2 != 0) return 0;
int targetSum = (sum + target) /2;
return (int) Math.pow(2, zeros) * countSubset(nums, targetSum);
}
private static int countSubset(int nums[], int target){
int n= nums.length;
int dp[][]= new int[n+1][target + 1];
//Intilization
for(int i=0; i<= n; i++) dp[i][0]= 1;
for(int j=0; j<= target; j++) dp[0][j]= 0;
dp[0][0]= 1;
//
for(int i=1; i<= n; i++){
for(int j=1; j<= target; j++){
if(nums[i-1] == 0){
dp[i][j]= dp[i-1][j];
continue;
}
if(j >= nums[i-1]){
dp[i][j]= dp[i-1][j- nums[i-1]] + dp[i-1][j];
}
else{
dp[i][j]= dp[i-1][j];
}
}
}
return dp[n][target];
}
public static void main(String[] args) {
int nums[]={1,1,1,1,1};
int target=3;
System.out.println(findTargetSumWays(nums, target));
// Input: nums = [1,1,1,1,1], target = 3
// Output: 5
// Explanation: There are 5 ways to assign symbols to make the sum of nums be target 3.
// -1 + 1 + 1 + 1 + 1 = 3
// +1 - 1 + 1 + 1 + 1 = 3
// +1 + 1 - 1 + 1 + 1 = 3
// +1 + 1 + 1 - 1 + 1 = 3
// +1 + 1 + 1 + 1 - 1 = 3
}
}