Backtracking & Exhaustive Search · 15 min · free with an account

Meet in the Middle: Mental Model

Lesson preview · about 30%

Enumerate two half-spaces and combine their summaries instead of exploring the full product directly.

Core idea

Preserve the invariant first; the implementation is a consequence of it.

Start with the idea

Begin with the concrete question: Return true if a subset sums to 15. For the visible input [7,3,2,9,5,11], the stated result is true. Before reading code, name the smallest piece of information that must survive from one step to the next. Most patterns here explore a decision tree; meet-in-the-middle instead enumerates two halves and combines them. Define the partial choice or subset summary that the pattern retains. That is the mental model for Meet in the Middle; it gives you a reason for each update instead of a line of code to memorise.

The core mechanism is this: Enumerate two half-spaces and combine their summaries instead of exploring the full product directly. Treat it as a claim to test after every operation. Write down the state before the first operation, update it once, and ask which part of the claim is still true. If an item is skipped, inserted, or removed, explain why that action cannot destroy an answer that should be retained. A useful trace has columns for the current input position, the state before the step, the action, and the state afterward. Use the visible sample until the expected result becomes inevitable rather than merely plausible.

Now inspect the Rust sketch, not just its final expression. The supplied Rust sketch is a starting point. Identify what it stores, how one iteration changes it, and whether it already solves the exact task or only demonstrates a related operation.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Algorithms