3.7.1 · D2Algorithm Paradigms

Visual walkthrough — Brute force — exhaustive search, when acceptable

1,973 words9 min readBack to topic

This is the visual backbone of the parent topic. If you want the Hindi walkthrough, see the Hinglish version.


Step 1 — The atom of everything: one choice, two boxes

WHAT. Before any formula, the smallest possible decision: you have one item, and it can go into one of two boxes — call them "IN" and "OUT". That's it. One item, two possibilities.

WHY start here? Because every brute-force count is secretly just this atom repeated. If you understand "1 item → 2 outcomes", you understand , and with a small twist, everything else. We refuse to write until you've watched where the comes from.

PICTURE. The red item drops into either the left box or the right box — two arrows, two futures.

Figure — Brute force — exhaustive search, when acceptable

Step 2 — Two items: choices multiply, they don't add

WHAT. Add a second item. Item A still has its 2 options (IN/OUT). For each of those, item B independently also has 2 options. So we don't get ; we get branches, each splitting into again → complete outcomes.

WHY multiply and not add? This is the single most important idea on the page, so let's earn it. "Add" would be right if the choices were alternatives (do A or do B). But here the choices stack — you decide A and then decide B. Every A-branch carries a full copy of the B-choice underneath it. Copies-of-copies is multiplication. Look at the tree: level 1 has 2 nodes, level 2 has children per node .

PICTURE. A branching tree. The red path traces one complete outcome (A=OUT, B=IN) out of the four leaves.

Figure — Brute force — exhaustive search, when acceptable

Step 3 — items with 2 options each → the subset count

WHAT. Now do items, every one an independent IN/OUT switch. Plug into the multiplication rule with every :

WHY does this equal "number of subsets"? A subset is exactly a choice of which elements are IN. So "list of IN/OUT switches" and "subset" are the same object wearing different clothes. Counting the switch-settings is counting the subsets — hence subsets, matching the parent note exactly.

PICTURE. A doubling staircase: 1 item → 2, 2 items → 4, 3 → 8, 4 → 16. The red curve rises through the flat black budget line — the moment it crosses is the moment brute force starts to time out.

Figure — Brute force — exhaustive search, when acceptable

Step 4 — Change the options-per-choice: alphabet , length

WHAT. Keep the multiplication rule but change how many options each choice has. A PIN of length over digits: each of the slots has options.

WHY is this the same formula as ? Because was just with , . Subsets are PINs over a 2-letter alphabet ("IN","OUT"). One rule, two costumes. For a 4-digit PIN, — tiny, so the parent's crack loop finishes instantly. That's why short PINs are weak.

PICTURE. A row of slots, each a red dial showing ticks; the caption multiplies them out.

Figure — Brute force — exhaustive search, when acceptable

Step 5 — When choices shrink: permutations give

WHAT. Now the twist. Arrange distinct items into ordered slots. Slot 1: any of items. But once one is placed, it's used up — slot 2 has only left, slot 3 has , … the last slot has . The options shrink by one each step:

WHY shrinking? Because reuse is forbidden — you can't place the same item twice. Contrast Step 4, where a digit could repeat (the same every slot). Repetition allowed → constant options → . Repetition forbidden → dwindling options → . The only difference between an exponential and a factorial is whether the pool refills.

PICTURE. A funnel of slots: the first slot fed by all red tokens, each later slot fed by a visibly smaller pool.

Figure — Brute force — exhaustive search, when acceptable

Step 6 — Unordered pairs: , then halve →

WHAT. Choose 2 items where order doesn't matter. Two sub-steps. First, ordered pairs: slot 1 = options, slot 2 = (no reuse) → . But and are the same unordered pair, so we've counted everything twice. Divide by 2:

WHY divide by exactly 2? Because a pair of 2 things can be written in orders, and every unordered pair shows up in all of its orderings among the ordered count. Two orderings collapse to one → divide by 2. This is precisely why the parent's Two-Sum loop writes for j in range(i+1, n) — starting at i+1 visits each unordered pair once, the built-in halving.

PICTURE. An grid of cells. The black diagonal (self-pairs , forbidden) is crossed out; the two triangles are mirror images; only the red upper triangle is kept.

Figure — Brute force — exhaustive search, when acceptable

Step 7 — Degenerate & edge cases (never leave a gap)

WHAT. The formulas must survive the weird inputs. Walk each:

Input Formula says Meaning — is it sane?
subsets Yes: the empty set is the one subset of nothing.
permutations Yes: exactly one way to arrange nothing (the empty arrangement).
pairs Yes: you can't pick 2 distinct items from 1.
PIN Yes: the empty string is the one length-0 PIN.
alphabet Yes: with one symbol, only one string exists.

WHY check these? A brute-force loop must not crash or miss on the boundary. range(1 << 0) is range(1) — it runs once, correctly enumerating the empty subset. If your formula gave there, your loop would skip a real candidate. The math and the loop agree at the edges — that's the safety proof.

PICTURE. A number line of showing each count landing on its true value, with the cases (which surprise people) circled in red.

Figure — Brute force — exhaustive search, when acceptable
Recall Why is

and not ? An arrangement of an empty set of items ::: there is exactly one — the arrangement that places nothing. Zero ways to fail leaves one valid (empty) outcome. It also keeps consistent at : .


The one-picture summary

Everything above is one rulemultiply the options at each step — bent four ways:

  • options constant at 2 (subsets)
  • options constant at (tuples/PINs)
  • options draining (permutations)
  • options draining for 2 steps, then halve (pairs)
Figure — Brute force — exhaustive search, when acceptable

The red curves (, ) rocket past the black budget line almost immediately; the black curves (, ) crawl. That gap is the entire reason Dynamic Programming, Greedy Algorithms, and pruning for NP-Hard Problems exist.

Recall Feynman: the whole walkthrough in plain words

Counting how many things brute force must try is always the same game: at each step, ask "how many ways can this step go?" and multiply those numbers together. If every step is a yes/no switch, you multiply a bunch of 2's and get — that's every subset. If each step is a digit, you multiply 10's and get — that's every PIN. If you're lining people up and can't reuse anyone, the number of choices shrinks each step — , then , then — and multiplying those gives . If you just want a pair and don't care about order, count them as ordered ( times ) and then cut in half because you counted each pair both ways. The scary part isn't the formulas — it's that and shoot up so fast that after about or so, "try everything" would take longer than the universe has been around. That single fact is why we ever bother inventing cleverer algorithms.