{"id":"combination-sum","title":"Combination Sum","difficulty":"medium","patterns":["backtracking"],"primaryConcept":"backtracking-subsets","concepts":["backtracking-subsets"],"problemForm":"core","companies":[],"category":"backtracking","compareMode":"unordered-nested-array","syntheticTestConfig":{"inputShape":"array-scalar","primaryParam":"candidates","secondaryParam":"target","sizeMetric":"length","sizes":[4,8,16,30],"constraints":["positive","distinct"],"valueRange":{"min":2,"max":40},"secondaryParams":{"target":{"strategy":"random-int","value":{"min":1,"max":40}}}},"description":"Given an array of distinct positive integers `candidates` and a positive integer `target`, return all unique combinations whose values sum to `target`.\n\nThe same candidate may be chosen any number of times. Return combinations in any order.","examples":[{"input":"candidates = [2,3,6,7], target = 7","output":"[[2,2,3],[7]]","explanation":"2 + 2 + 3 and 7 are the only unique combinations that reach 7."},{"input":"candidates = [2,3,5], target = 8","output":"[[2,2,2,2],[2,3,3],[3,5]]","explanation":"Each combination may reuse a candidate, but duplicate orderings are not repeated."},{"input":"candidates = [2], target = 1","output":"[]","explanation":"No positive combination can reach the target."}],"constraints":[{"id":"1-len-candidates-30","text":"1 <= len(candidates) <= 30","check":{"kind":"sizeRange","target":"candidates","measure":"arrayLength","min":1,"max":30}},{"id":"2-candidates-i-40","text":"2 <= candidates[i] <= 40","check":{"kind":"valueRange","target":"candidates","selector":"arrayItems","min":2,"max":40}},{"id":"all-candidates-are-distinct","text":"All candidates are distinct","check":{"kind":"uniqueness","target":"candidates","selector":"items"}},{"id":"1-target-40","text":"1 <= target <= 40","check":{"kind":"valueRange","target":"target","selector":"value","min":1,"max":40}},{"id":"the-number-of-unique-combinations-is-less-than-150","text":"The number of unique combinations is less than 150"}],"starterCode":"class Solution:\n    def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:\n        # Your code here\n        pass","functionName":"combinationSum","testCases":[{"id":"1","input":{"candidates":[2,3,6,7],"target":7},"expected":[[2,2,3],[7]],"description":"Canonical two-combination case."},{"id":"2","input":{"candidates":[2,3,5],"target":8},"expected":[[2,2,2,2],[2,3,3],[3,5]],"description":"Several combinations with reuse."},{"id":"3","input":{"candidates":[2],"target":1},"expected":[],"description":"No valid combination."},{"id":"4","input":{"candidates":[7,3,2],"target":7},"expected":[[7],[3,2,2]],"description":"Input order does not matter for comparing combinations."},{"id":"5","input":{"candidates":[4,6,8],"target":8},"expected":[[4,4],[8]],"description":"A direct hit and a repeated candidate both qualify."},{"id":"h1","input":{"candidates":[2],"target":4},"expected":[[2,2]],"description":"A single candidate can be reused repeatedly."},{"id":"h2","input":{"candidates":[2,3,5],"target":5},"expected":[[2,3],[5]],"description":"Two different ways to reach the same target."},{"id":"h3","input":{"candidates":[2,4,6],"target":7},"expected":[],"description":"No combination reaches the target."},{"id":"h4","input":{"candidates":[2,3,6,7],"target":9},"expected":[[2,2,2,3],[2,7],[3,3,3],[3,6]],"description":"A target with both short and repeated-combination answers."},{"id":"h5","input":{"candidates":[4,5,6,7],"target":11},"expected":[[4,7],[5,6]],"description":"Two disjoint pairs reach the same sum."},{"id":"h6","input":{"candidates":[3,5,8],"target":8},"expected":[[3,5],[8]],"description":"The target can be reached either by one candidate or by combining two smaller candidates."},{"id":"h7","input":{"candidates":[2,3,5],"target":10},"expected":[[2,2,2,2,2],[2,2,3,3],[2,3,5],[5,5]],"description":"Multiple repeated-use combinations reach the same larger target."}],"hints":[{"level":1,"content":"Build one partial combination at a time and stop extending it when its remaining target becomes negative.","revealPattern":false},{"level":2,"content":"Use a start index so recursive calls can reuse the current candidate but cannot go back to earlier candidates.","revealPattern":true},{"level":3,"content":"After appending a candidate, recurse with remaining - candidate and then pop it before trying the next choice.","revealPattern":true}],"guidedFlow":{"approachSelection":{"contract":"approach-selection-v1","question":"Which approach should we implement?","decisionPrompt":"Choose the approach whose state and complexity match the problem constraints.","approaches":[{"id":"all-sequences","name":"Generate ordered sequences","summary":"Try every order of values and filter sums at the end.","decisionReason":"It builds every ordering of candidates and filters, so it re-discovers the same combination in many orders and wastes most of the work.","complexity":{"time":"Exponential with many duplicates","space":"O(target / min(candidates))"},"tradeoffs":["Runs in Exponential with many duplicates time and uses O(target / min(candidates)) space."],"isOptimal":false},{"id":"backtracking-start-index","name":"Backtracking with start index","summary":"Carry a start index and remaining sum, allowing reuse of the current index while preventing duplicate orderings.","decisionReason":"Grow one combination by subtracting a candidate from the remaining target, recursing, then undoing; a start index allows reuse while ruling out reordered duplicates.","complexity":{"time":"Output-sensitive exponential","space":"O(target / min(candidates))"},"tradeoffs":["Runs in Output-sensitive exponential time and uses O(target / min(candidates)) space."],"isOptimal":true}]},"codingCheckpoints":[{"id":"init-result","title":"Initialize the result accumulator","description":"Create the list that will collect complete combinations.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"initializes_result_accumulator","scope":"init-result","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Initialize a result accumulator for emitted search solutions.","doneWhen":"A collection is initialized before recursive search begins and is returned or otherwise used as the solution store.","shouldFailWhen":"The code has no collection dedicated to storing complete combinations.","roles":["result accumulator"],"mustProve":["result accumulator initialization"],"mustNotAccept":["temporary path initialization only"],"proofSnippets":[{"id":"py-init-result","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    return result"}]},"commonMistakes":["Returning only the current path","Reinitializing the result inside recursion"],"hint":"Use one outer result list that every terminal branch can append to."},{"id":"define-backtrack","title":"Define the recursive helper","description":"Create a helper that carries the start index and remaining target.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"defines_recursive_helper","scope":"define-backtrack","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Define a recursive helper for exploring the choice tree.","doneWhen":"A helper exists and can call itself recursively.","shouldFailWhen":"The code has only flat loops and no recursive search helper.","roles":["recursive helper"],"mustProve":["recursive helper definition","recursive self-call"],"mustNotAccept":["non-recursive helper"],"proofSnippets":[{"id":"py-define-backtrack","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    def backtrack(start, remaining):\n        backtrack(start, remaining)\n    return result"}]},"commonMistakes":["Trying to enumerate all lengths with nested loops","Not carrying enough state into recursion"],"hint":"The helper needs to remember which candidate index can be chosen next."},{"id":"base-case","title":"Emit complete combinations","description":"When the remaining target is zero, append a snapshot of the current path and return.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"handles_terminal_solution_case","scope":"base-case","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Emit a snapshot of the current search state at a terminal solution branch.","doneWhen":"A completion branch appends a copied path to the result and exits that branch.","shouldFailWhen":"The terminal branch appends the mutable path itself or keeps exploring after a complete sum.","roles":["completion condition","path state","result accumulator"],"mustProve":["terminal completion branch","solution snapshot emission","terminal return"],"mustNotAccept":["emitting a live mutable path"],"proofSnippets":[{"id":"py-base-case","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        backtrack(start, remaining)\n    return result"}]},"commonMistakes":["Appending path without copying it","Forgetting to return after recording a complete combination"],"hint":"Store a snapshot, because the path will be mutated again after recursion unwinds."},{"id":"loop-choices","title":"Loop over allowed choices","description":"For each recursive state, try candidates from the current start index onward.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"iterates_choice_space","scope":"loop-through-elements","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Iterate the candidate choice space from the current boundary.","doneWhen":"The recursive helper contains a choice loop over candidate indices or values.","shouldFailWhen":"The helper checks only one candidate or recurses without enumerating choices.","roles":["choice loop","choice boundary"],"mustProve":["candidate choice-space iteration"],"mustNotAccept":["single fixed candidate check"],"proofSnippets":[{"id":"py-loop-choices","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        for i in range(start, len(candidates)):\n            backtrack(i, remaining)\n    return result"}]},"commonMistakes":["Always restarting from index 0","Only checking the current candidate once"],"hint":"The loop begins at the carried start index."},{"id":"choose-explore-unchoose","title":"Choose, recurse, and unchoose","description":"Append the candidate, recurse with a reduced remaining target, and pop the candidate afterward.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"choose_explore_unchoose","scope":"include-and-backtrack","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Mutate search state for a candidate, recurse with progressed state, and roll back the mutation.","doneWhen":"Each choice appends to the path, recurses with the reduced target, and removes the appended value after recursion.","shouldFailWhen":"The code recurses without undoing the path mutation or does not reduce the carried target.","roles":["path state","choice value","remaining target"],"mustProve":["choice mutation","choice-coupled recursive state reduction","post-recursive cleanup"],"mustNotAccept":["recursing with unchanged remaining target","missing cleanup"],"proofSnippets":[{"id":"py-choose-explore-unchoose","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        for i in range(start, len(candidates)):\n            path.append(candidates[i])\n            backtrack(i, remaining - candidates[i])\n            path.pop()\n    return result"}]},"commonMistakes":["Recursing with i + 1 and disallowing reuse","Forgetting to pop after recursion","Recursing without reducing the remaining target"],"hint":"Reuse is allowed, so the recursive call keeps index i but lowers the remaining target."},{"id":"initial-call","title":"Kick off the search","description":"Start recursion at index 0 with the original target.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"kicks_off_recursive_search","scope":"initial-call","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Start the recursive search from the first candidate with the full target still remaining.","doneWhen":"After defining the helper, the code calls it with start index 0 and the original target value.","shouldFailWhen":"The helper is never called, starts from a later index, or starts with a modified target.","roles":["recursive helper","start index","remaining target"],"mustProve":["recursive search kickoff","zero start index","original target remaining value"],"mustNotAccept":["unreachable helper","starting from a later index","starting from a modified target"],"proofSnippets":[{"id":"py-initial-call","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        for i in range(start, len(candidates)):\n            path.append(candidates[i])\n            backtrack(i, remaining - candidates[i])\n            path.pop()\n    backtrack(0, target)"}]},"commonMistakes":["Never calling the helper","Starting from a later index","Starting with a modified target"],"hint":"The first call should represent an empty path and the full target."},{"id":"return-result","title":"Return all combinations","description":"Return the result accumulator after the recursive search completes.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"returns_solution_accumulator","scope":"return-result","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Return the accumulated list of complete combinations after the search has run.","doneWhen":"The recursive search is kicked off and the outer function returns the result accumulator.","shouldFailWhen":"The code returns the current path, a count, or the accumulator before starting the search.","roles":["recursive helper","result accumulator","terminal return"],"mustProve":["search kickoff before return","return of solution accumulator"],"mustNotAccept":["returning the current path","returning only a count","returning before running search"],"proofSnippets":[{"id":"py-return-result","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        for i in range(start, len(candidates)):\n            path.append(candidates[i])\n            backtrack(i, remaining - candidates[i])\n            path.pop()\n    backtrack(0, target)\n    return result"}]},"commonMistakes":["Returning the current path","Returning only the count of combinations"],"hint":"The result list holds every emitted combination."}],"version":2,"progressRevision":"combination-sum-2026-07-19-derived-start-index","slides":[{"id":"read-the-signal","kind":"info","chapter":"The combinations","eyebrow":"Read the problem","title":"Every way to hit the target","workspacePanel":"problem","body":["You get distinct positive candidates and a target. Find every combination of candidates that sums to the target, and a candidate may be used as many times as you like.","A candidate may be reused, but rearranging the same chosen values does not make a new combination. The search has to allow repeats without emitting reordered copies."]},{"id":"brute-force","kind":"code-trace","chapter":"The obvious way","eyebrow":"Try everything","title":"Grow a running sum from any candidate","workspacePanel":"trace","summary":"The first instinct: from any partial sum, try every candidate next, and keep the orderings that land on the target.","codeTrace":{"contract":"code-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"def combinationSum(candidates: list[int], target: int) -> list[list[int]]:\n    result: list[list[int]] = []\n    def grow(path: list[int], remaining: int) -> None:\n        if remaining == 0:\n            result.append(list(path))\n            return\n        if remaining < 0:\n            return\n        for i in range(len(candidates)):\n            path.append(candidates[i])\n            grow(path, remaining - candidates[i])\n            path.pop()\n    grow([], target)\n    return result","steps":[{"id":"grow-2","title":"Pick a 2, remaining 5","codeLine":9,"summary":"From an empty path the loop is free to pick any candidate; here it takes the 2.","state":{"nums":[2,3,6,7],"stack":[2],"stackActive":[0],"remaining":5,"i":0,"labels":{"stack":"path"}}},{"id":"grow-2-2-3","title":"One order that lands: [2, 2, 3]","codeLine":5,"summary":"Two 2s and a 3 drive remaining to 0, so this ordering is recorded.","state":{"nums":[2,3,6,7],"stack":[2,2,3],"stackActive":[2],"remaining":0,"i":1,"labels":{"stack":"path"}}},{"id":"grow-3-2-2","title":"And [3, 2, 2] lands too","codeLine":5,"summary":"Because every pick may start from index 0, the same three values arrive in a new order and are recorded again.","state":{"nums":[2,3,6,7],"stack":[3,2,2],"stackActive":[2],"remaining":0,"i":0,"labels":{"stack":"path"}}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"const result = [];\nfunction grow(path, remaining) {\n    if (remaining === 0) {\n        result.push(Array.from(path));\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (let i = 0; i < candidates.length; i++) {\n        path.push(candidates[i]);\n        grow(path, (remaining - candidates[i]));\n        path.pop();\n    }\n}\ngrow([], target);\nreturn result;","lineMap":{"2":1,"4":3,"5":4,"6":5,"7":7,"8":8,"9":10,"10":11,"11":12,"12":13,"13":16,"14":17},"statementBundles":{"2":{"startLine":1,"endLine":2},"6":{"startLine":5,"endLine":6},"8":{"startLine":8,"endLine":9},"12":{"startLine":13,"endLine":15}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"const result: Array<Array<number>> = [];\nfunction grow(path: Array<number>, remaining: number): void {\n    if (remaining === 0) {\n        result.push(Array.from(path));\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (let i = 0; i < candidates.length; i++) {\n        path.push(candidates[i]);\n        grow(path, (remaining - candidates[i]));\n        path.pop();\n    }\n}\ngrow([], target);\nreturn result;","lineMap":{"2":1,"4":3,"5":4,"6":5,"7":7,"8":8,"9":10,"10":11,"11":12,"12":13,"13":16,"14":17},"statementBundles":{"2":{"startLine":1,"endLine":2},"6":{"startLine":5,"endLine":6},"8":{"startLine":8,"endLine":9},"12":{"startLine":13,"endLine":15}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"List<List<int>> result = new();\nvoid Grow(List<int> path, int remaining) {\n    if (remaining == 0) {\n        result.Add(new List<int>(path));\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (int i = 0; i < candidates.Count; i++) {\n        path.Add(candidates[i]);\n        Grow(path, remaining - candidates[i]);\n        path.RemoveAt(path.Count - 1);\n    }\n}\nGrow(new List<int>(), target);\nreturn result;","lineMap":{"2":1,"4":3,"5":4,"6":5,"7":7,"8":8,"9":10,"10":11,"11":12,"12":13,"13":16,"14":17},"statementBundles":{"2":{"startLine":1,"endLine":2},"6":{"startLine":5,"endLine":6},"8":{"startLine":8,"endLine":9},"12":{"startLine":13,"endLine":15}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"function<void(vector<int>&, int)> grow;\n\nvector<vector<int>> result;\ngrow = [&]( vector<int>& path, int remaining ) -> void {\n    if (remaining == 0) {\n        result.push_back(path);\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (int i = 0; i < candidates.size(); i++) {\n        path.push_back(candidates[i]);\n        grow(path, (remaining - candidates[i]));\n        path.pop_back();\n    }\n};\nvector<int> path = vector<int>{};\ngrow(path, target);\nreturn result;","lineMap":{"2":3,"4":5,"5":6,"6":7,"7":9,"8":10,"9":12,"10":13,"11":14,"12":15,"13":18,"14":20},"statementBundles":{"2":{"startLine":3,"endLine":4},"6":{"startLine":7,"endLine":8},"8":{"startLine":10,"endLine":11},"12":{"startLine":15,"endLine":17},"13":{"startLine":18,"endLine":19}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"private static void grow(List<Integer> path, int remaining, List<List<Integer>> result, List<Integer> candidates) {\n    if (remaining == 0) {\n        result.add(new ArrayList<Integer>(path));\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (int i = 0; i < candidates.size(); i++) {\n        path.add(candidates.get(i));\n        grow(path, (remaining - candidates.get(i)), result, candidates);\n        path.remove(path.size() - 1);\n    }\n}\n\nList<List<Integer>> result = new ArrayList<>();\ngrow(new ArrayList<>(), target, result, candidates);\nreturn result;","lineMap":{"2":16,"4":2,"5":3,"6":4,"7":6,"8":7,"9":9,"10":10,"11":11,"12":12,"13":17,"14":18},"statementBundles":{"6":{"startLine":4,"endLine":5},"8":{"startLine":7,"endLine":8},"12":{"startLine":12,"endLine":14}}}}}},{"id":"brute-force-cost","kind":"case-probe","chapter":"The obvious way","eyebrow":"Count the waste","title":"How many orderings does this record for the values 2, 2, 3?","workspacePanel":"trace","summary":"Letting every pick start from index 0 treats order as meaningful.","options":[{"id":"three","label":"Three: [2, 2, 3], [2, 3, 2], and [3, 2, 2]","rationale":"Restarting from index 0 every time, the same multiset of values arrives in each distinct arrangement, so one real combination is recorded three times.","isCorrect":true},{"id":"one","label":"One, the combination is recorded once","rationale":"Nothing here pins an order, so every arrangement of the same values reaches the target and is recorded separately."},{"id":"two","label":"Two, since two of the values are equal","rationale":"Equal values still land in different positions across the orderings, so all three arrangements are recorded, not two."}],"answer":"Three. With every pick free to start at index 0, [2, 2, 3], [2, 3, 2], and [3, 2, 2] all land, so one combination is found three times over."},{"id":"subtract","kind":"case-probe","chapter":"The idea","eyebrow":"Constrain the search","title":"What restriction keeps [2, 2, 3] but removes its reordered copies?","workspacePanel":"trace","summary":"After path [2], another 2 must still be possible. After path [3], choosing 2 would only recreate an ordering already explored.","options":[{"id":"subtract","label":"Keep choices from the current index onward, including the current value","rationale":"Keeping the current index allows another 2. Closing earlier indices after moving forward fixes one non-decreasing order, so [2, 2, 3] remains possible while [3, 2, 2] does not.","isCorrect":true},{"id":"same","label":"Restart from index 0 after every pick","rationale":"Restarting reopens earlier candidates after later ones, which recreates [2, 3, 2] and [3, 2, 2]."},{"id":"grow","label":"Move to the next index after every pick","rationale":"Moving past the current index forbids reuse, so the valid combination [2, 2, 3] disappears."}],"answer":"Carry a start index and keep choices from that index onward. Recurse from the same index to allow reuse; when a branch advances, earlier candidates stay closed, so each combination has one non-decreasing order.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"subtract-state","state":{"nums":[2,3,6,7],"stack":[2],"stackActive":[0],"remaining":5,"i":0,"labels":{"stack":"path"}}}]}},{"id":"remaining-state","kind":"case-probe","chapter":"The idea","eyebrow":"Track what is left","title":"The target is 7 and the path just chose 2. What smaller search remains?","workspacePanel":"visualizer","summary":"The chosen 2 has already covered part of the target. The recursive state should describe only what is still missing.","options":[{"id":"remaining-five","label":"Find combinations totaling 5 using the choices still open","rationale":"Using 2 covers part of 7, so this branch is now the same search with remaining 5 and the current start index.","isCorrect":true},{"id":"full-target","label":"Keep searching for 7 and check the path sum only at the end","rationale":"Carrying the remaining amount makes the smaller subproblem explicit and gives the recursion a direct stopping condition."},{"id":"larger-target","label":"Search for 9 because the path now contains one value","rationale":"The original target stays fixed; choosing 2 uses part of it, so the amount still needed decreases rather than grows."}],"answer":"The remaining target is 5. Picking a candidate turns the branch into the same search on a smaller amount: remaining = 7 - 2.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"remaining-state-view","state":{"nums":[2,3,6,7],"stack":[2],"stackActive":[0],"remaining":5,"i":0,"labels":{"stack":"path"}}}]}},{"id":"base-cases","kind":"case-probe","chapter":"The idea","eyebrow":"Find the ends","title":"A branch ends in two ways. What are they?","workspacePanel":"visualizer","summary":"You keep subtracting candidates from the remaining target.","options":[{"id":"zero-neg","label":"Remaining hits exactly 0 (record it), or goes negative (dead end)","rationale":"Hitting 0 means the picks sum exactly to the target, a valid combination; going below 0 means the last pick overshot, so the branch is abandoned.","isCorrect":true},{"id":"empty","label":"The candidates run out","rationale":"Candidates can be reused, so they never \"run out\"; what ends a branch is the remaining reaching 0 or going negative."},{"id":"len","label":"The combination reaches a fixed length","rationale":"Combinations have no fixed length here; a branch ends when the remaining target reaches 0."}],"answer":"Remaining 0 is a hit, recorded; remaining below 0 overshot, a dead end. The running amount alone decides when to stop.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"base-state","state":{"nums":[2,3,6,7],"stack":[2,2,3],"stackActive":[2],"remaining":0,"i":1,"labels":{"stack":"path"}}}]}},{"id":"approach-selection","kind":"choice","source":"approach-selection","chapter":"The idea","eyebrow":"Name what you built","title":"Which search shape matches the rules we just derived?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"approach-structure","state":{"nums":[2,3,6,7],"stack":[],"stackActive":[],"remaining":7,"i":0,"labels":{"stack":"path"}}}]}},{"id":"dry-run-setup","kind":"info","chapter":"Trace","eyebrow":"Watch it move","title":"Subtract, recurse, undo","workspacePanel":"visualizer","body":["The path holds the current combination and remaining holds what is left of the target. Picking a candidate appends it and subtracts it from remaining.","Tracing candidates [2, 3, 6, 7] toward target 7, watch one branch build [2, 2, 3] as remaining counts down to 0."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"setup-state","state":{"nums":[2,3,6,7],"stack":[],"stackActive":[],"remaining":7,"i":0,"labels":{"stack":"path"}}}]}},{"id":"trace","kind":"dry-run","source":"trace-example","chapter":"Trace","eyebrow":"Build a combination","title":"Build a combination to the target","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"pick-2","state":{"nums":[2,3,6,7],"stack":[2],"stackActive":[0],"remaining":5,"i":0,"labels":{"stack":"path"}}},{"id":"pick-2-again","state":{"nums":[2,3,6,7],"stack":[2,2],"stackActive":[1],"remaining":3,"i":0,"labels":{"stack":"path"}}},{"id":"pick-3","state":{"nums":[2,3,6,7],"stack":[2,2,3],"stackActive":[2],"remaining":0,"i":1,"labels":{"stack":"path"}}}]},"beats":[{"id":"pick-2","title":"After picking 2, what is the new remaining?","summary":"The first choice in this branch.","prompt":"Target is 7, and the candidate is 2.","options":[{"id":"five","label":"5, the target minus 2","rationale":"Using the 2 covers 2 of the 7, leaving 5 still to reach.","isCorrect":true},{"id":"seven","label":"7, unchanged until the end","rationale":"The remaining drops as you pick, which is what makes each step a smaller subproblem."},{"id":"two","label":"2, the candidate value","rationale":"Remaining tracks what is left of the target. After this pick, 7 - 2 = 5."}],"answer":"5. Picking the 2 leaves 7 - 2 = 5 of the target to reach."},{"id":"pick-2-again","title":"Remaining is 5. Can the next pick be the 2 again?","summary":"The recursion restarted from the 2's own index.","prompt":"After the first 2, the next choice is being made.","options":[{"id":"yes","label":"Yes: the same index is in range, so remaining drops to 3","rationale":"Recursing at the 2’s own index keeps it available, so a second 2 is picked and 5 - 2 = 3 remains.","isCorrect":true},{"id":"no","label":"No: each candidate is used once","rationale":"This problem allows reuse, and starting from the same index is exactly what permits a second 2."},{"id":"back","label":"Only by going back to an earlier candidate","rationale":"No going back is needed; the 2 is still in range from the current start index."}],"answer":"Yes. The start index still includes the 2, so it is picked again and remaining falls to 3."},{"id":"pick-3","title":"Remaining is 3 and the next pick is the 3. What now?","summary":"The third choice in this branch.","prompt":"Remaining was 3, and the 3 is the candidate in hand.","options":[{"id":"record","label":"Record [2, 2, 3] as a valid combination","rationale":"Remaining of exactly 0 means the picks sum to the target, so this combination is saved before backtracking.","isCorrect":true},{"id":"continue","label":"Keep picking to be sure","rationale":"Remaining is 0, so any further pick would overshoot; the combination is complete and recorded."},{"id":"discard","label":"Discard it, the path is too long","rationale":"Length does not matter; summing exactly to the target is what makes it valid."}],"answer":"Record it. Remaining 0 means [2, 2, 3] sums to 7 exactly, so it is saved, then the branch unwinds."}]},{"id":"no-reuse-pitfall","kind":"pitfall","chapter":"Trace","eyebrow":"Spot the trap","workspacePanel":"trace","title":"Can the pop after recursion be dropped?","summary":"Appending a candidate, recursing, then popping it looks like three lines where the pop only resets work that is already done. The append and recurse clearly do the search, so the pop reads as bookkeeping you could skip. Watch what the path holds once a finished branch hands control back to its parent.","pitfall":"The code below appends and recurses but never pops the candidate after the recursive call returns.","whyItMatters":"Without the pop, a finished branch leaves its values in the shared path, so the parent's next choice is appended onto stale picks. The branch that should have been [2, 2, 6] becomes [2, 2, 3, 6], and every sibling inherits the leak. The pop is what restores the path to exactly the parent's choices before the next candidate is tried.","negativeTrace":{"contract":"negative-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"def combinationSum(candidates: list[int], target: int) -> list[list[int]]:\n    result: list[list[int]] = []\n    path: list[int] = []\n    def backtrack(start: int, remaining: int) -> None:\n        if remaining == 0:\n            result.append(list(path))\n            return\n        if remaining < 0:\n            return\n        for i in range(start, len(candidates)):\n            path.append(candidates[i])\n            backtrack(i, remaining - candidates[i])\n    backtrack(0, target)\n    return result","steps":[{"id":"record","title":"A branch lands: [2, 2, 3] is recorded","codeLine":6,"summary":"Two 2s and a 3 reach remaining 0, so the path is recorded and this branch returns.","state":{"nums":[2,3,6,7],"stack":[2,2,3],"stackActive":[2],"remaining":0,"i":1,"labels":{"stack":"path"}}},{"id":"no-pop","title":"Control returns, but the 3 was never removed","codeLine":12,"summary":"Back in the remaining-3 frame, the loop moves to the next candidate while the 3 still sits in the path.","state":{"nums":[2,3,6,7],"stack":[2,2,3],"stackActive":[2],"remaining":3,"i":2,"labels":{"stack":"path"}}},{"id":"leak","title":"The 6 is appended onto stale picks: [2, 2, 3, 6]","codeLine":11,"summary":"The parent meant to try [2, 2, 6], but the leftover 3 is still there, so the path no longer describes this branch's choices.","state":{"nums":[2,3,6,7],"stack":[2,2,3,6],"stackActive":[3],"remaining":-3,"i":2,"labels":{"stack":"path"}}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"const result = [];\nconst path = [];\nfunction backtrack(start, remaining) {\n    if (remaining === 0) {\n        result.push(Array.from(path));\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (let i = start; i < candidates.length; i++) {\n        path.push(candidates[i]);\n        backtrack(i, (remaining - candidates[i]));\n    }\n}\nbacktrack(0, target);\nreturn result;","lineMap":{"2":1,"3":2,"5":4,"6":5,"7":6,"8":8,"9":9,"10":11,"11":12,"12":13,"13":16,"14":17},"statementBundles":{"3":{"startLine":2,"endLine":3},"7":{"startLine":6,"endLine":7},"9":{"startLine":9,"endLine":10},"12":{"startLine":13,"endLine":15}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"const result: Array<Array<number>> = [];\nconst path: Array<number> = [];\nfunction backtrack(start: number, remaining: number): void {\n    if (remaining === 0) {\n        result.push(Array.from(path));\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (let i = start; i < candidates.length; i++) {\n        path.push(candidates[i]);\n        backtrack(i, (remaining - candidates[i]));\n    }\n}\nbacktrack(0, target);\nreturn result;","lineMap":{"2":1,"3":2,"5":4,"6":5,"7":6,"8":8,"9":9,"10":11,"11":12,"12":13,"13":16,"14":17},"statementBundles":{"3":{"startLine":2,"endLine":3},"7":{"startLine":6,"endLine":7},"9":{"startLine":9,"endLine":10},"12":{"startLine":13,"endLine":15}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"List<List<int>> result = new();\nList<int> path = new();\nvoid Backtrack(int start, int remaining) {\n    if (remaining == 0) {\n        result.Add(new List<int>(path));\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (int i = start; i < candidates.Count; i++) {\n        path.Add(candidates[i]);\n        Backtrack(i, remaining - candidates[i]);\n    }\n}\nBacktrack(0, target);\nreturn result;","lineMap":{"2":1,"3":2,"5":4,"6":5,"7":6,"8":8,"9":9,"10":11,"11":12,"12":13,"13":16,"14":17},"statementBundles":{"3":{"startLine":2,"endLine":3},"7":{"startLine":6,"endLine":7},"9":{"startLine":9,"endLine":10},"12":{"startLine":13,"endLine":15}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"function<void(int, int)> backtrack;\n\nvector<vector<int>> result;\nvector<int> path;\nbacktrack = [&]( int start, int remaining ) -> void {\n    if (remaining == 0) {\n        result.push_back(path);\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (int i = start; i < candidates.size(); i++) {\n        path.push_back(candidates[i]);\n        backtrack(i, (remaining - candidates[i]));\n    }\n};\nbacktrack(0, target);\nreturn result;","lineMap":{"2":3,"3":4,"5":6,"6":7,"7":8,"8":10,"9":11,"10":13,"11":14,"12":15,"13":18,"14":19},"statementBundles":{"3":{"startLine":4,"endLine":5},"7":{"startLine":8,"endLine":9},"9":{"startLine":11,"endLine":12},"12":{"startLine":15,"endLine":17}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"private static void backtrack(int start, int remaining, List<List<Integer>> result, List<Integer> path, List<Integer> candidates) {\n    if (remaining == 0) {\n        result.add(new ArrayList<Integer>(path));\n        return;\n    }\n    if (remaining < 0) {\n        return;\n    }\n    for (int i = ((Number)start).intValue(); i < candidates.size(); i++) {\n        path.add(candidates.get(i));\n        backtrack(i, (remaining - candidates.get(i)), result, path, candidates);\n    }\n}\n\nList<List<Integer>> result = new ArrayList<>();\nList<Integer> path = new ArrayList<>();\nbacktrack(0, target, result, path, candidates);\nreturn result;","lineMap":{"2":15,"3":16,"5":2,"6":3,"7":4,"8":6,"9":7,"10":9,"11":10,"12":11,"13":17,"14":18},"statementBundles":{"7":{"startLine":4,"endLine":5},"9":{"startLine":7,"endLine":8},"12":{"startLine":11,"endLine":13}}}}}},{"id":"edge-case-intro","kind":"info","chapter":"Edge cases","eyebrow":"Try to break it","title":"Which inputs could break this?","workspacePanel":"visualizer","body":["Subtracting a candidate from the remaining target and recursing from the same start index built every valid combination without repeats.","Now probe the boundaries. First a target smaller than every candidate, then a single-candidate input whose value divides the target evenly."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"intro-state","state":{"nums":[2,3,6,7],"stack":[],"stackActive":[],"remaining":7,"i":0,"labels":{"stack":"path"}}}]}},{"id":"target-too-small-edge","kind":"case-probe","chapter":"Edge cases","eyebrow":"Stress the rule","title":"When no candidate fits the target, how many combinations remain?","workspacePanel":"visualizer","summary":"candidates = [5, 7], target = 3.","options":[{"id":"none","label":"Zero, the result is empty","rationale":"Both 5 and 7 exceed 3, so the very first pick sends remaining negative and every branch dies before recording anything.","isCorrect":true},{"id":"one","label":"One, the empty combination","rationale":"The empty combination sums to 0, not 3, so it is not recorded; nothing reaches the target."},{"id":"two","label":"Two, one per candidate","rationale":"Neither candidate alone equals 3, and both overshoot, so no combination is valid."}],"answer":"Zero. Every candidate overshoots 3, so the first pick always goes negative and nothing is ever recorded.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"small-state","state":{"nums":[5,7],"stack":[5],"stackActive":[0],"remaining":-2,"i":0,"labels":{"stack":"path"}}}]}},{"id":"exact-divisor-edge","kind":"case-probe","chapter":"Edge cases","eyebrow":"Stress the rule","title":"One candidate divides the target evenly. What is found?","workspacePanel":"visualizer","summary":"candidates = [3], target = 9.","options":[{"id":"three","label":"One combination, [3, 3, 3]","rationale":"Reuse lets the single 3 be picked three times, dropping remaining 9 to 0, the only way to hit the target.","isCorrect":true},{"id":"one","label":"One combination, [3]","rationale":"A single 3 leaves remaining 6, not 0; the candidate must be reused until the target is met."},{"id":"none","label":"None, only one candidate","rationale":"One candidate is enough when it can be reused; [3, 3, 3] sums to 9."}],"answer":"One: [3, 3, 3]. Reuse picks the 3 three times to drive remaining from 9 down to 0.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"divisor-state","state":{"nums":[3],"stack":[],"stackActive":[],"remaining":9,"i":0,"labels":{"stack":"path"}}}]}},{"id":"ready-to-code","kind":"info","chapter":"Code","eyebrow":"Get ready to code","title":"Turn the rule into code","workspacePanel":"visualizer","summary":"Remaining and the start index carry the whole search.","body":["Let the remaining target decide when a branch is done: record it at 0 and abandon it once it goes negative.","Let the start index control reuse and ordering: each choice tries a candidate, explores its completions, and undoes itself before the next, never reaching back before the start."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"ready-state","state":{"nums":[2,3,6,7],"stack":[],"stackActive":[],"remaining":7,"i":0,"labels":{"stack":"path"}}}]}},{"id":"coding","kind":"coding","source":"coding-checkpoints","chapter":"Code","title":"Build the backtracking search","eyebrow":"Code the rule","summary":"Now write it: start a result list, then a recursive helper carrying the start index and remaining; record the path when remaining hits 0 and abandon it once it goes negative, loop the candidates from the start index, and for each one append it, recurse on the smaller remaining, and pop it back off. Kick off at index 0 with the full target, then return the list.","workspacePanel":"code","checkpoints":[{"id":"init-result","title":"Initialize the result accumulator","description":"Create the list that will collect complete combinations.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"initializes_result_accumulator","scope":"init-result","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Initialize a result accumulator for emitted search solutions.","doneWhen":"A collection is initialized before recursive search begins and is returned or otherwise used as the solution store.","shouldFailWhen":"The code has no collection dedicated to storing complete combinations.","roles":["result accumulator"],"mustProve":["result accumulator initialization"],"mustNotAccept":["temporary path initialization only"],"proofSnippets":[{"id":"py-init-result","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    return result"}]},"commonMistakes":["Returning only the current path","Reinitializing the result inside recursion"],"hint":"Use one outer result list that every terminal branch can append to."},{"id":"define-backtrack","title":"Define the recursive helper","description":"Create a helper that carries the start index and remaining target.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"defines_recursive_helper","scope":"define-backtrack","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Define a recursive helper for exploring the choice tree.","doneWhen":"A helper exists and can call itself recursively.","shouldFailWhen":"The code has only flat loops and no recursive search helper.","roles":["recursive helper"],"mustProve":["recursive helper definition","recursive self-call"],"mustNotAccept":["non-recursive helper"],"proofSnippets":[{"id":"py-define-backtrack","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    def backtrack(start, remaining):\n        backtrack(start, remaining)\n    return result"}]},"commonMistakes":["Trying to enumerate all lengths with nested loops","Not carrying enough state into recursion"],"hint":"The helper needs to remember which candidate index can be chosen next."},{"id":"base-case","title":"Emit complete combinations","description":"When the remaining target is zero, append a snapshot of the current path and return.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"handles_terminal_solution_case","scope":"base-case","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Emit a snapshot of the current search state at a terminal solution branch.","doneWhen":"A completion branch appends a copied path to the result and exits that branch.","shouldFailWhen":"The terminal branch appends the mutable path itself or keeps exploring after a complete sum.","roles":["completion condition","path state","result accumulator"],"mustProve":["terminal completion branch","solution snapshot emission","terminal return"],"mustNotAccept":["emitting a live mutable path"],"proofSnippets":[{"id":"py-base-case","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        backtrack(start, remaining)\n    return result"}]},"commonMistakes":["Appending path without copying it","Forgetting to return after recording a complete combination"],"hint":"Store a snapshot, because the path will be mutated again after recursion unwinds."},{"id":"loop-choices","title":"Loop over allowed choices","description":"For each recursive state, try candidates from the current start index onward.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"iterates_choice_space","scope":"loop-through-elements","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Iterate the candidate choice space from the current boundary.","doneWhen":"The recursive helper contains a choice loop over candidate indices or values.","shouldFailWhen":"The helper checks only one candidate or recurses without enumerating choices.","roles":["choice loop","choice boundary"],"mustProve":["candidate choice-space iteration"],"mustNotAccept":["single fixed candidate check"],"proofSnippets":[{"id":"py-loop-choices","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        for i in range(start, len(candidates)):\n            backtrack(i, remaining)\n    return result"}]},"commonMistakes":["Always restarting from index 0","Only checking the current candidate once"],"hint":"The loop begins at the carried start index."},{"id":"choose-explore-unchoose","title":"Choose, recurse, and unchoose","description":"Append the candidate, recurse with a reduced remaining target, and pop the candidate afterward.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"choose_explore_unchoose","scope":"include-and-backtrack","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Mutate search state for a candidate, recurse with progressed state, and roll back the mutation.","doneWhen":"Each choice appends to the path, recurses with the reduced target, and removes the appended value after recursion.","shouldFailWhen":"The code recurses without undoing the path mutation or does not reduce the carried target.","roles":["path state","choice value","remaining target"],"mustProve":["choice mutation","choice-coupled recursive state reduction","post-recursive cleanup"],"mustNotAccept":["recursing with unchanged remaining target","missing cleanup"],"proofSnippets":[{"id":"py-choose-explore-unchoose","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        for i in range(start, len(candidates)):\n            path.append(candidates[i])\n            backtrack(i, remaining - candidates[i])\n            path.pop()\n    return result"}]},"commonMistakes":["Recursing with i + 1 and disallowing reuse","Forgetting to pop after recursion","Recursing without reducing the remaining target"],"hint":"Reuse is allowed, so the recursive call keeps index i but lowers the remaining target."},{"id":"initial-call","title":"Kick off the search","description":"Start recursion at index 0 with the original target.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"kicks_off_recursive_search","scope":"initial-call","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Start the recursive search from the first candidate with the full target still remaining.","doneWhen":"After defining the helper, the code calls it with start index 0 and the original target value.","shouldFailWhen":"The helper is never called, starts from a later index, or starts with a modified target.","roles":["recursive helper","start index","remaining target"],"mustProve":["recursive search kickoff","zero start index","original target remaining value"],"mustNotAccept":["unreachable helper","starting from a later index","starting from a modified target"],"proofSnippets":[{"id":"py-initial-call","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        for i in range(start, len(candidates)):\n            path.append(candidates[i])\n            backtrack(i, remaining - candidates[i])\n            path.pop()\n    backtrack(0, target)"}]},"commonMistakes":["Never calling the helper","Starting from a later index","Starting with a modified target"],"hint":"The first call should represent an empty path and the full target."},{"id":"return-result","title":"Return all combinations","description":"Return the result accumulator after the recursive search completes.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"backtracking-search","fact":"returns_solution_accumulator","scope":"return-result","readMode":"backtracking-search-value-query-v1"}]},"semanticSpec":{"intent":"Return the accumulated list of complete combinations after the search has run.","doneWhen":"The recursive search is kicked off and the outer function returns the result accumulator.","shouldFailWhen":"The code returns the current path, a count, or the accumulator before starting the search.","roles":["recursive helper","result accumulator","terminal return"],"mustProve":["search kickoff before return","return of solution accumulator"],"mustNotAccept":["returning the current path","returning only a count","returning before running search"],"proofSnippets":[{"id":"py-return-result","language":"python","code":"def combinationSum(candidates, target):\n    result = []\n    path = []\n    def backtrack(start, remaining):\n        if remaining == 0:\n            result.append(path.copy())\n            return\n        for i in range(start, len(candidates)):\n            path.append(candidates[i])\n            backtrack(i, remaining - candidates[i])\n            path.pop()\n    backtrack(0, target)\n    return result"}]},"commonMistakes":["Returning the current path","Returning only the count of combinations"],"hint":"The result list holds every emitted combination."}]},{"id":"complete","kind":"complete","chapter":"Code","title":"What to carry forward","eyebrow":"Take it with you","summary":"Grow one combination by subtracting candidates from the remaining target, and let a start index handle reuse and duplicates.","workspacePanel":"code","body":["Backtracking is choose, explore, undo: append a candidate and recurse on the smaller remaining, then pop to try the next.","The start index does double duty: recursing at i keeps a candidate available for reuse, and never going below the start kills reordered duplicates."],"takeaways":[{"title":"Remaining is the whole state","body":"A branch records at remaining 0 and dies below 0, so the running target alone decides every stopping point."},{"title":"The start index controls order and reuse","body":"Recurse from i (not i + 1) to allow reuse; never look before the start index so each set is built in one fixed order."},{"title":"The cost is output-sensitive and exponential","body":"Work scales with the number of combinations emitted, with recursion depth bounded by target / min(candidates); the start index keeps that cost down by never re-exploring the permutation duplicates the brute force paid for."}],"nextAction":"Submit when you are ready. TraceCode will run the final checks against the code you wrote."}],"slidesOrigin":"authored"},"executionStyle":"solution-method"}
