forked from v100901/hackoctoberfest2020__
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarget_Sum.cpp
More file actions
32 lines (28 loc) · 706 Bytes
/
Target_Sum.cpp
File metadata and controls
32 lines (28 loc) · 706 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void solve(vector<int>& nums, int target, int i, int &ans){
if(i<0) return;
if(i==0 && nums[i]==abs(target)){
if(target==0) ans += 2;
else ans++;
return;
}
solve(nums, target-nums[i], i-1, ans);
solve(nums, target+nums[i], i-1, ans);
}
int findTargetSumWays(vector<int>& nums, int target) {
int n = nums.size();
int ans = 0;
solve(nums, target, n-1, ans);
return ans;
}
int main(){
int n;
cin>>n;
vector<int> nums(n);
for(int i=0;i<n;i++) cin>>nums[i];
int target;
cin>>target;
int ans = findTargetSumWays(nums, target);
cout<<ans;
}