Verify and Solve a Subset Sum Instance
Use binary input length correctly and prove a dynamic-programming recurrence.
Assignment sources
Problem
For positive integers , define to mean that some subset of the first numbers sums to . Derive a recurrence, solve the instance , and explain why the algorithm is pseudopolynomial rather than polynomial in the binary input length.
Hint 1
A solution either excludes or includes it and previously formed .
Hint 2
The table has a column for every integer from through , while binary encoding of uses only bits.
Full solution
Initialize and for . For ,
The two disjuncts exactly partition subsets by whether they contain , proving correctness by induction on .
For the instance, , so is true; backtracking yields the selected subset .
The table has entries and can be filled in arithmetic/Boolean steps. This is polynomial in the numeric value , but may be exponential in its binary length. Therefore this runtime does not establish that binary SUBSET-SUM is in P. Membership in NP follows separately from a selection-bit certificate and polynomial-time binary addition.
Checkpoint
“Pseudopolynomial” describes dependence on encoded numeric values; it is not the same as exponential on every instance.
Provenance note
Original exercise using the standard dynamic program. It does not rely on student submissions as sources.
Common mistakes
- Skipping a direction of correctness or an input-format edge case.
- Measuring resources against a numeric value rather than encoded input length.
- Treating an informal intuition as a proof of the stated bound.
Sources and verification
- 2026 transcript 15
- Garey-Johnson, SP13
- Corpus coverage note: No direct merged-summary coverage.
- Catalog mapping:
data/exercises.json; statements and solutions are independently written; personal submissions are non-authoritative.
Subset Sum dynamic programming
Open in the visualization gallery: Subset Sum dynamic programming