a single padding element, |2S−T|

SUBSET-SUM ≤p PARTITION

SUBSET-SUM≤pPARTITION\mathrm{SUBSET\text{-}SUM}\le_p\mathrm{PARTITION}, so PARTITION\mathrm{PARTITION} is NP-hard.

The construction

The derivation — the part not to skip. SUBSET-SUM\mathrm{SUBSET\text{-}SUM} has an external target SS; PARTITION\mathrm{PARTITION} has none — its target is dictated from within and is necessarily half the total. The reduction must "bake the SS in".

Write T=∑iaiT=\sum_i a_i. Suppose for a moment — purely to think, not as part of the algorithm — that some II has ∑i∈Iai=S\sum_{i\in I}a_i=S. The other side sums to T−ST-S. Add an element bb to the "small" side. In the case S>T/2S>T/2 we require ∑i∈Iai=S\sum_{i\in I}a_i=S and ∑i∉Iai+b=S\sum_{i\notin I}a_i+b=S, whence b=S−(∑iai⏟=T−∑i∈Iai⏟=S)=2S−T.b=S-\Bigl(\underbrace{\textstyle\sum_i a_i}_{=T}-\underbrace{\textstyle\sum_{i\in I}a_i}_{=S}\Bigr)=2S-T .

And here is the beauty. The middle expression S−∑i∉IaiS-\sum_{i\notin I}a_i is not computable by the reduction: it depends on II, which is exactly the object we do not have. But the final expression 2S−T2S-T depends only on the input — SS is given and TT takes linear time. The intermediate question moved from "depends on the solution" to "depends on the input", and with that the reduction became legal. Symmetrically, if S<T/2S<T/2 we get b=T−2Sb=T-2S.

Algorithm RR. On input (a1,…,an,S)(a_1,\dots,a_n,S):

  1. Compute T←∑iaiT\leftarrow\sum_i a_i.
  2. If 2S=T2S=T: return (a1,…,an)(a_1,\dots,a_n) unchanged.
  3. If 2S>T2S>T: return (a1,…,an, 2S−T)(a_1,\dots,a_n,\,2S-T).
  4. If 2S<T2S<T: return (a1,…,an, T−2S)(a_1,\dots,a_n,\,T-2S).

The comparison is written as 2S2S against TT rather than SS against 12T\tfrac12T so as to stay in the integers when TT is odd; it is the same comparison. When TT is odd, case 2 simply cannot occur. In the other two cases b>0b>0 strictly, precisely because of the case condition.

Visualization

Guided construction

Subset Sum → Partition

Step 1 of 6
A = {3, 5, 6, 11}T = 14
Subset Sum to Partition via two added markersBars proportional to 3, 5, 6, 11 and to the markers 39 and 36 fill two pans that each total 50.Numbers drawn to scale35611
Original number

Correctness — both directions

SUBSET-SUM ∈ ⟹ PARTITION ∈

Suppose (a1,…,an,S)∈SUBSET-SUM(a_1,\dots,a_n,S)\in\mathrm{SUBSET\text{-}SUM}; some II has ∑i∈Iai=S\sum_{i\in I}a_i=S. Split by the case the algorithm took.

Case 2S=T2S=T. The output is (a1,…,an)(a_1,\dots,a_n). The set II satisfies ∑i∈Iai=S=T/2\sum_{i\in I}a_i=S=T/2, so ∑i∉Iai=T−T/2=T/2\sum_{i\notin I}a_i=T-T/2=T/2 — the two sides are equal, and the output is in PARTITION\mathrm{PARTITION}.

Case 2S>T2S>T. The output is (a1,…,an,b)(a_1,\dots,a_n,b) with b=2S−T>0b=2S-T>0 and total T+b=2ST+b=2S. Take the side II itself: ∑i∈Iai=S=12(2S)\sum_{i\in I}a_i=S=\tfrac12(2S) — exactly half the new total. So the output is in PARTITION\mathrm{PARTITION}. (The other side is {ai}i∉I\{a_i\}_{i\notin I} together with bb, summing to (T−S)+(2S−T)=S(T-S)+(2S-T)=S.)

Case 2S<T2S<T. The output is (a1,…,an,b)(a_1,\dots,a_n,b) with b=T−2S>0b=T-2S>0 and total T+b=2T−2ST+b=2T-2S. Take the side I∪{b}I\cup\{b\}: it sums to S+(T−2S)=T−S=12(2T−2S)S+(T-2S)=T-S=\tfrac12(2T-2S) — exactly half. So the output is in PARTITION\mathrm{PARTITION}.

PARTITION ∈ ⟹ SUBSET-SUM ∈

Suppose the output is in PARTITION\mathrm{PARTITION}; it splits into two sides of equal sum. Split by case again.

Case 2S=T2S=T. The output is the original input, and an equal split means some II has ∑i∈Iai=T/2=S\sum_{i\in I}a_i=T/2=S. So (a1,…,an,S)∈SUBSET-SUM(a_1,\dots,a_n,S)\in\mathrm{SUBSET\text{-}SUM}.

Case 2S>T2S>T. The output totals 2S2S, so each side sums to SS. Consider the side not containing the padding element bb (exactly one of the two sides contains it). That side consists solely of original elements; let II index them. Then ∑i∈Iai=S\sum_{i\in I}a_i=S, so (a1,…,an,S)∈SUBSET-SUM(a_1,\dots,a_n,S)\in\mathrm{SUBSET\text{-}SUM}.

Case 2S<T2S<T. The output totals 2T−2S2T-2S, so each side sums to T−ST-S. Consider the side that does contain b=T−2Sb=T-2S, and let JJ index its original elements. Then ∑i∈Jai+(T−2S)=T−S⟹∑i∈Jai=S.\sum_{i\in J}a_i+(T-2S)=T-S\quad\Longrightarrow\quad\sum_{i\in J}a_i=S . So (a1,…,an,S)∈SUBSET-SUM(a_1,\dots,a_n,S)\in\mathrm{SUBSET\text{-}SUM}.

In all three cases we obtained a subset of the original elements summing to SS, as required.

Cost of the reduction

TimeO(|x|²)
SpaceO(|x|)
Output sizeO(|x|)

Let mm be the longest encoding among a1,…,an,Sa_1,\dots,a_n,S, and ∣x∣≥n+m|x|\ge n+m the input size. Step 1 performs n−1n-1 additions; each partial sum is at most n⋅10mn\cdot10^m, so of length ≤m+⌈log⁡10n⌉+1\le m+\lceil\log_{10}n\rceil+1, and one addition is linear in the lengths — O(n(m+log⁡n))=O(∣x∣2)O(n(m+\log n))=O(|x|^2) overall. Steps 2–4 are a single comparison and subtraction.

Running time is measured in encoding length, not in the numbers' values: had we needed to count up to TT, the reduction would not be polynomial. The output length is O(∣x∣)O(|x|) — which matters for transitivity, since a reduction that inflates its input exponentially is useless under composition.

What it buys

PARTITION\mathrm{PARTITION} is NP-hard, and lies in NP\mathrm{NP}, hence NP-complete. From there the chain continues to BIN-PACKING\mathrm{BIN\text{-}PACKING}.

Pitfalls

  • Why from SUBSET-SUM\mathrm{SUBSET\text{-}SUM} rather than SAT\mathrm{SAT}. SAT\mathrm{SAT} would also work — any NP-complete problem reduces to any other — but "encoding a formula as a set of numbers" is wasted labour. The principle: move between problems in the same world. Here numbers go in and numbers come out.

  • The algorithm computes TT and compares — that is allowed. What it does not do is test whether a subset summing to SS exists, and it cannot. The element an+1a_{n+1} helps decide nothing; it only guarantees "if there was a solution before, there is one now, and conversely".

  • The output is a list of n+1n+1 numbers, not a pair (numbers, target). That is exactly what a PARTITION\mathrm{PARTITION} input is; nobody promised nn would stay fixed.

The chain

Where this was read from

  • Lesson 12משפט 2 (לב השיעור): SUBSET-SUM≤pPARTITION\mathrm{SUBSET\text{-}SUM} \le_p \mathrm{PARTITION}
  • Guided exercisepartition-from-subset-sum
  • 2026 '1 מועד 2Subset-SumSubset\text{-}Sum against PartitionPartition, and the reduction between them.
  • 2025 '2 מועד 1Defining PartitionPartition in decimal representation and proving it hard.