-
Sort the Array:
candidates = [1,1,2,5,6,7,10]
-
Initial Call: backtrack(0, 8)
-
First Level (start = 0, target = 8):
- i = 0: Candidate = 1
- Include 1:
current = [1]
- Recurse:
backtrack(1, 7)
-
Second Level (start = 1, target = 7):
- i = 1: Candidate = 1
- Include 1:
current = [1,1]
- Recurse:
backtrack(2, 6)
-
Third Level (start = 2, target = 6):
- i = 2: Candidate = 2
- Include 2:
current = [1,1,2]
- Recurse:
backtrack(3, 4)
-
Fourth Level (start = 3, target = 4):
- i = 3: Candidate = 5
- 5 > 4, break the loop.
- Backtrack to
current = [1,1,2]
-
Back to Third Level (start = 2, target = 6):
- i = 3: Candidate = 5
- Include 5:
current = [1,1,5]
- Recurse:
backtrack(4, 1)
-
Fifth Level (start = 4, target = 1):
- i = 4: Candidate = 6
- 6 > 1, break the loop.
- Backtrack to
current = [1,1,5]
-
Back to Third Level (start = 2, target = 6):
- i = 4: Candidate = 6
- Include 6:
current = [1,1,6]
- Recurse:
backtrack(5, 0)
-
Sixth Level (start = 5, target = 0):
target === 0
, add [1,1,6]
to ans
- Backtrack to
current = [1,1]
-
Back to Second Level (start = 1, target = 7):
- i = 2: Candidate = 2
- Include 2:
current = [1,1,2]
- Recurse:
backtrack(3, 5)
-
Third Level (start = 3, target = 5):
- i = 3: Candidate = 5
- Include 5:
current = [1,1,2,5]
- Recurse:
backtrack(4, 0)
-
Seventh Level (start = 4, target = 0):
target === 0
, add [1,1,2,5]
to ans
- Backtrack to
current = [1,1,2]
-
Back to Third Level (start = 3, target = 5):
- i = 4: Candidate = 6
- 6 > 5, break the loop.
- Backtrack to
current = [1,1,2]
-
Back to Second Level (start = 1, target = 7):
- i = 3: Candidate = 5
- Include 5:
current = [1,1,5]
- Recurse:
backtrack(4, 2)
-
Third Level (start = 4, target = 2):
- i = 4: Candidate = 6
- 6 > 2, break the loop.
- Backtrack to
current = [1,1,5]
-
Back to Second Level (start = 1, target = 7):
- i = 4: Candidate = 6
- Include 6:
current = [1,1,6]
- Recurse:
backtrack(5, 1)
-
Fourth Level (start = 5, target = 1):
- i = 5: Candidate = 7
- 7 > 1, break the loop.
- Backtrack to
current = [1,1,6]
-
Back to Second Level (start = 1, target = 7):
- i = 5: Candidate = 7
- Include 7:
current = [1,1,7]
- Recurse:
backtrack(6, 0)
-
Eighth Level (start = 6, target = 0):
target === 0
, add [1,1,7]
to ans
- Backtrack to
current = [1,1]
-
Back to Second Level (start = 1, target = 7):
- i = 6: Candidate = 10
- 10 > 7, break the loop.
- Backtrack to
current = [1,1]
-
Back to First Level (start = 0, target = 8):
- i = 1: Candidate = 1 (Duplicate of previous, skip)
- i = 2: Candidate = 2
- Include 2:
current = [1,2]
- Recurse:
backtrack(3, 6)
-
Second Level (start = 3, target = 6):
- i = 3: Candidate = 5
- Include 5:
current = [1,2,5]
- Recurse:
backtrack(4, 1)
-
Third Level (start = 4, target = 1):
- i = 4: Candidate = 6
- 6 > 1, break the loop.
- Backtrack to
current = [1,2,5]
-
Back to Second Level (start = 3, target = 6):
- i = 4: Candidate = 6
- Include 6:
current = [1,2,6]
- Recurse:
backtrack(5, 0)
-
Fourth Level (start = 5, target = 0):
target === 0
, add [1,2,6]
to ans
- Backtrack to
current = [1,2]
-
Back to Second Level (start = 3, target = 6):
- i = 5: Candidate = 7
- 7 > 6, break the loop.
- Backtrack to
current = [1,2]
-
Back to First Level (start = 0, target = 8):
- i = 3: Candidate = 5
- Include 5:
current = [1,5]
- Recurse:
backtrack(4, 3)
-
Second Level (start = 4, target = 3):
- i = 4: Candidate = 6
- 6 > 3, break the loop.
- Backtrack to
current = [1,5]
-
Back to First Level (start = 0, target = 8):
- i = 4: Candidate = 6
- Include 6:
current = [1,6]
- Recurse:
backtrack(5, 2)
-
Second Level (start = 5, target = 2):
- i = 5: Candidate = 7
- 7 > 2, break the loop.
- Backtrack to
current = [1,6]
-
Back to First Level (start = 0, target = 8):
- i = 5: Candidate = 7
- Include 7:
current = [1,7]
- Recurse:
backtrack(6, 1)
-
Third Level (start = 6, target = 1):
- i = 6: Candidate = 10
- 10 > 1, break the loop.
- Backtrack to
current = [1,7]
-
Back to First Level (start = 0, target = 8):
- i = 6: Candidate = 10
- 10 > 8, break the loop.
- Backtrack to
current = []
-
Final Result: [[1,1,6], [1,2,5], [1,7], [2,6]]
The recursive backtracking approach explores all possible combinations by making a binary choice at each step: include the current candidate or exclude it.
Understanding this approach not only helps in solving similar combinatorial problems but also reinforces fundamental concepts in recursion, backtracking, and algorithm optimization. Implementing the solution in a functional style without using classes makes the code more concise and easier to understand, especially for beginners. By systematically breaking down the problem and applying strategic decision-making, complex problems become manageable and solvable with clarity and confidence.