The three steps
Core1) do an operation → 2) recurse → 3) undo the operation. The analogy from session: you lend a friend ₹500 (step 1), they do the task (step 2 = recursive call), you take the ₹500 back (step 3 = the undo). Step 3 restores state to exactly what it was before step 1.
- Way 1 — explicit undo:
index++; recurse;index--. Mutate, recurse, revert. - Way 2 — pass-forward: recurse with
index + 1directly; no undo needed because you never mutated the caller's state. - Both are correct. Pick whichever fits. For list/string state (add element,
recurse, remove element) you still explicitly undo; for a plain integer index
you can usually just pass
i + 1.
⭐ TRICK The loop-start rule (biggest
transferable insight). Look at the output's 1-D arrays. If every
subset/permutation has the same length → start the inner loop at
0 (permutations). If they have variable length → start the
loop at start/index and pass i + 1 (or
i) forward (subsets, combination sum). This single observation tells
you the loop shape before you write any code.