MCPcopy Create free account
hub / github.com/chaharnishant11/CodeIn10DSA / Solution

Class Solution

Backtracking/Homework/combinationSum.cpp:1–33  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2public:
3 vector<vector<int>> ans;
4
5 void helper(vector<int>& candidates, int target,int i,int curSum, vector<int> temp){
6 // Bounding Condition
7 if (curSum>target){
8 return;
9 }
10 // Base Condition
11 if (i==candidates.size()){
12 if(curSum==target){
13 ans.push_back(temp);
14 }
15 return;
16 }
17 // Inclusion
18 curSum+=candidates[i];
19 temp.push_back(candidates[i]);
20 helper(candidates,target,i,curSum,temp);
21 curSum-=candidates[i];
22 temp.pop_back();
23
24 // Exclusion
25 helper(candidates,target,i+1,curSum,temp);
26 }
27 vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
28 vector<int> temp;
29 helper(candidates,target,0,0,temp);
30 return ans;
31
32 }
33};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected