-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTragetSum.cpp
More file actions
51 lines (43 loc) · 1.2 KB
/
TragetSum.cpp
File metadata and controls
51 lines (43 loc) · 1.2 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
//PROBLEM:You are given an integer array nums and an integer target.
// You want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenate all the integers.
// For example, if nums = [2, 1], you can add a '+' before 2 and a '-' before 1 and concatenate them to build the expression "+2-1".
// Return the number of different expressions that you can build, which evaluates to target.
#include<bits/stdc++.h>
using namespace std;
int countSubsetSum(int a[],int sum,int n)
{
int t[n+1][sum+1];
t[0][0]=1;
for(int i=1;i<n+1;i++)
t[i][0]=1;
for(int i=1;i<sum+1;i++)
t[0][i]=0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= sum; j++)
{
// if the value is greater than the sum
if (a[i - 1] > j)
t[i][j] = t[i - 1][j];
else
{
t[i][j] = t[i - 1][j] + t[i - 1][j - a[i - 1]];
}
}
}
return t[n][sum];
}
int main()
{
int a[]={1,1,1,1,1};
int target=3;
int n=sizeof(a)/sizeof(a[0]);
int sum=0;
for(int i=0;i<n;i++)
{
sum+=a[i];
}
int val=(sum+target)/2;
cout<<(countSubsetSum(a,val,n));
return 0;
}