02.02 · Lesson
Backtracking: Choose, Explore, Undo
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
Item 13 of 44 · updated 2026-10-13