Build an answer one choice at a time and undo the ones that lead nowhere: choose, recurse, then un-choose so the next choice starts clean.
▼Transcript
Welcome to the Backtracking world. Build an answer one choice at a time, and when a choice leads nowhere, undo it and try the next.
Picture a cave of forks. The explorer picks a tunnel and goes deeper, dropping breadcrumbs. Dead end? Step back, pick up the crumbs, try the next. A path that reaches a gem gets recorded.
In code, three beats. Choose: add a number to the path. Explore: recurse, one level deeper. Un-choose: pop it off, so the next choice starts clean.
Try it on one, two, three, recording the path at every step: empty, one, one two, one two three. Pop the three, pop the two, and choose three: one three. Then two, two three, and three: eight subsets.
The clue: the problem asks for all or every, or says generate, with combinations, permutations or subsets, and a small n.
The cost is exponential: each item is in or out, so n items make two to the n subsets. Three make eight; thirty make over a billion. Keep n small.
Now let's use it on Subsets.