Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created September 21, 2019 09:11
Show Gist options
  • Save surinoel/cda61c93bfff1c3eccd790d0e705de3b to your computer and use it in GitHub Desktop.
Save surinoel/cda61c93bfff1c3eccd790d0e705de3b to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
using namespace std;
void go(int idx, int sum, const vector<int> &numbers, int &cnt, int target) {
if(idx == numbers.size()) {
if(sum == target) {
cnt += 1;
}
return;
}
go(idx + 1, sum + numbers[idx], numbers, cnt, target);
go(idx + 1, sum - numbers[idx], numbers, cnt, target);
}
int solution(vector<int> numbers, int target) {
int answer = 0;
go(0, 0, numbers, answer, target);
return answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment