solulubeta
Sign in
Recursion & Memo / Choices and backtracking
02.02 · Lesson

Backtracking: Choose, Explore, Undo

When a step has many options, loop over them inside the recursion: choose, explore, undo. Covers combination sum, permutations, skipping duplicates and stopping early.

11 min 2 videos Quiz Pro

Pick or skip gives two choices per step. Some problems have many choices per step: the next number can be any candidate, or the next position can hold any unused element. Then you loop over the choices.

The template

void solve(state, path) {
    if (done) { save a copy of path; return; }
    for (each choice c) {
        if (c is not allowed) continue;
        choose(c);          // change path
        solve(next state);  // explore
        undo(c);            // put path back
    }
}
solulu Pro

Continue this lesson with Pro

Inside: 7 sections · 2 videos · quiz.

  • The template
  • Example 1: combination sum (numbers can repeat)
  • Example 2: permutations
  • Skipping duplicate answers
  • Stop early
  • Which template?
  • +1 more
Lifetime ₹1,999 ₹999 · Launch offer
Mark this as done when you are finished.

Item 13 of 44 · updated 2026-10-13