How this theory page differs from the visualizer
Here you learn why one integer can stand for a whole set, and why clearing the lowest bit turns that integer into a DP state. The visualizer plays the same three walks one executed line at a time.
Intuition: First, pack four items into one number
Four items sit in a row and each is worth something: 5, 3, 8 and 2. You have used items 0, 1 and 3. Instead of keeping a list, write one bit per item, with item 0 at the right end and item 3 at the left, so the row of bits reads as an ordinary binary number: 1011. That number is a mask, and it is the whole used-set.
bit = item 3 2 1 0 value 2 8 3 5 used? 1 0 1 1 -> mask 1011 = 11 total 2 3 5 -> 2 + 3 + 5 = 10
The columns run from bit 3 on the left to bit 0 on the right, which is how a binary string is written, so the 1 in the rightmost column is bit 0 and it is worth 5.
The key choice: What one bit means, and what one move costs
Three operations are all a mask needs. Adding item i sets bit i with mask | (1 << i). Asking whether item i is in the set reads (mask >> i) & 1. And taking the lowest set bit apart uses low = mask & -mask with rest = mask & (mask - 1).
mask 1011 low 0001 the lowest set bit rest 1010 the same mask with that bit cleared
The third operation is the one that turns a set into a DP. Clearing a bit always produces a smaller number, so if masks are priced in increasing order, the smaller mask has already been answered. The step then costs one addition: the value of the bit that was cleared, plus what the rest of the mask was worth.
numberOfTrailingZeros.The learning journey: One recurrence, three ways to read it
Let n be the number of items. There are 2^n masks, so for four items there are 16, and the full mask is 1111 = 15.
1 · Naive recursion
Ask a mask for its total, clear the lowest bit, and ask the smaller mask. Nothing is kept, so a mask that another chain passes through is priced again.
Cost: one call per mask on the chain, so a mask with k set bits opens k + 1 calls (its own, and one for the empty set it ends on). The walk is 24 steps. It spends only the call stack.
2 · Top-down memo
The same calls, with each answer stored the first time. Every mask is then priced once, and any later chain that reaches it reads the stored number.
Cost: 25 steps here, which is one more than the naive walk rather than fewer. This recurrence has a single chain, so the memo costs a step per mask and saves nothing. It spends one slot per mask.
3 · Bottom-up table
Fill every mask from 0 upwards. Since each mask reads a smaller mask, the answer it needs is already in the table, and there is no recursion at all.
Cost: 83 steps over all 16 masks, one addition per mask, so the work grows with 2^n rather than n · 2^n: clearing the lowest bit costs one operation, not a scan of every bit. It spends a table of numbers.
The shared foundation: What is a mask worth?
The question. Given a mask, what do the values of the items whose bits are set add up to? Write the answer dp[mask].
The base. Mask 0 packs no items, so dp[0] = 0. That is the only state that needs no candidate, and every chain ends there.
dp[0] = 0 low = mask & -mask rest = mask & (mask - 1) bit = numberOfTrailingZeros(low) dp[mask] = dp[rest] + value[bit] answer = dp[used]
Why each read points where it does. For mask = 1011 the lowest set bit is 0001, so bit = 0 and rest = 1010. The value at bit 0 is 5, and dp[1010] holds the total of the items still in the set: bits 3 and 1, worth 2 + 3 = 5. So dp[1011] = 5 + 5 = 10. Every read is a mask, which is what makes one pass in increasing order enough.
Compare implementations: See how the same recurrence evolves
All three programs return 10 for the default used-set, and each one is a walk the visualizer can play: the tab names are the options in its Walk picker.
The idea: ask the smaller mask
One call prices one mask. It clears the lowest bit, reads the item behind it, and adds the answer for the mask that is left.
total(mask) = value[lowestBit(mask)] + total(mask & (mask - 1)) total(0) = 0
Cost: one call per set bit plus the empty set at the end, so at most n + 1 calls for one mask. The walk is 24 steps. Nothing is stored.
int total(int mask) { if (mask == 0) { return 0; } int low = mask & -mask; int bit = Integer.numberOfTrailingZeros(low); int value = items[bit]; return value + total(mask & (mask - 1)); } // no table: every caller prices its mask from scratch
A worked decision: How dp[1011] reaches 10
The used-set is read once every mask is written, so the number it names is already there. On the default sample the walk prices all 16 masks, then reads dp[1011].
mask 1011 low 0001 the lowest set bit bit 0 item 0, worth 5 rest 1010 the same mask without bit 0 read dp[1010] = 5 bits 3 and 1, worth 2 + 3 total 5 + 5 = 10 -> dp[1011] = 10
The verdict. The candidate reaches 10, and it is the only candidate the state weighs, because clearing the lowest bit is a determined move rather than a choice. Clearing the highest bit instead would give dp[0011] = 8 plus 2, which is also 10: the total does not depend on the order the bits are cleared.
Reading the result back: Which items make up the 10?
Reading a mask back is the same operation the recurrence uses, repeated until nothing is left: clear the lowest bit, name the item, subtract nothing, and keep going.
1011: the lowest bit is0001, so item0is in the set and worth 5. Rest1010.1010: the lowest bit is0010, so item1is in the set and worth 3. Rest1000.1000: the lowest bit is1000, so item3is in the set and worth 2. Rest0000.0000: nothing is left, so the read-back stops. Items 0, 1 and 3, and .
Summary: Which approach should you use?
| Approach | Time | Space | Best for |
|---|---|---|---|
| Naive recursion | one call per set bit plus the empty set, no memory between calls: 24 steps | the call stack | reading the recurrence as the definition it is |
| Top-down memo | the same 25 steps with a store; nothing to save on this recurrence | a memo of 2^n slots, started at -1 | the problems in this family where a mask has several parents |
| Bottom-up table | all 16 masks, one addition per mask (2^n): 83 steps | a table of 2^n numbers | the version to carry away, and the one the walk draws |
memo[mask] != 0 would treat a real answer as missing and recompute it, and is the mark that cannot be a total while every value is non-negative. The other common slip is filling the table downwards: every candidate reads a smaller mask, so a descending fill reads slots nobody has written.Check your understanding: Two questions, and one line to remember
Why does clearing the lowest set bit turn a mask into a DP state?
What does the memo walk save on this particular recurrence?
mask & (mask - 1) clears the lowest set bit, and dp[mask] = dp[rest] + value[bit]. Fill masks from 0 upwards and read the mask the question names.Put it into motion: Now watch all three walks
The visualizer writes the same used-set four ways at once: the items in a row with what each one is worth, then the mask in hand, the mask - 1 the identity subtracts, the mask & (mask - 1) that clears the lowest bit, and the mask & -mask that is that bit on its own. The four rows line up column by column, so the bit being cleared is a digit turning off rather than a claim that it did. Beside them sits the array, one row per mask, and every written row carries the read that filled it (dp[rest] + values[low] = total). A filled row can be clicked to jump back to the step that wrote it. On the two recursion walks the band is named for what the walk keeps, the memo's empty slots show the -1 they really hold, and the open calls are listed under the table.
On the default sample:
- Bottom-up table is 83 steps: five opening steps, the setup, the empty-set seed, then every mask priced with one candidate, and the answer.
- Top-down memo is 25 steps and naive recursion is 24. Watch the calls counter on both: the memo walk answers the same number of calls, because this recurrence has no repeats.
- The samples are
Four items, three used(10),Three items, all used(19), (2) and (0, the base case on its own).