# LeetCode: Combination Sum Solution

Recursively try all the possible cases

## Approach

Exhausively try all the cases

combination
: store the list of candidates for each check

remaining
: check whether a
combination
sum equals to target

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var combinationSum = function (candidates, target) {2  const res = []3
4  const recursion = ({ candidates, remaining, combination }) => {5    if (remaining < 0) {6      return7    }8    if (remaining === 0) {9      res.push(combination)10      return11    }12    candidates.forEach((candidate, i) =>13      recursion({14        candidates: candidates.slice(i), // a candidate could be reused15        remaining: remaining - candidate,16        combination: [...combination, candidate],17      })18    )19  }20
21  recursion({ candidates, remaining: target, combination: [] })22
23  return res24}```

Original problem

## Similar problems

Letter Combinations of a Phone Number

Combination Sum II

Combinations

Combination Sum III

Factor Combinations

Combination Sum IV

leetcode

recursion

backtracking

## Next Post

LeetCode: Is Subsequence

Aug 12, 2022

The author is not smart enough to come up with the DP solution in the first place

## Previous Post

LeetCode: Make Array Zero by Subtracting Equal Amounts

Jul 31, 2022

Count non-zero unique numbers

Search Posts