Use backtracking to explore all combinations, starting from the smallest candidate and allowing reuse of the same number. Sort the array first to handle duplicates and enable pruning when the current sum exceeds the target. At each step, choose a number, recurse with the same index (to allow reuse), and backtrack to explore other choices.
Pro tip: Emphasize the importance of sorting and pruning to avoid unnecessary recursive calls, and discuss how the solution can be adapted if the input contains duplicates. Also, mention that the time complexity is exponential in the worst case, but pruning significantly reduces the search space.
Confirm that the array contains distinct integers and that numbers can be reused. Sort the array to enable efficient pruning and consistent order.
Create a recursive function that takes the current index, remaining target, and current combination. At each call, iterate from the current index to the end of the array.
For each candidate, if it is less than or equal to the remaining target, add it to the combination and recurse with the same index (to allow reuse) and updated remaining target.
After returning from recursion, remove the last added number to backtrack. If the candidate exceeds the remaining target, break out of the loop since the array is sorted.
When the remaining target becomes zero, add a copy of the current combination to the result list. Return the result after exploring all possibilities.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.