How meet-in-the-middle splits a subset sum

Short answer: Meet-in-the-middle solves subset sum by splitting the items into two halves. Each half has about n/2 elements, so each half has 2^{n/2} subset sums. Sort one list. For every sum on the other list, binary search the complement that comes closest to the target without passing it. The time is O(n · 2^{n/2}) and the memory is Θ(2^{n/2}). The dynamic program is faster when the target is small. This split is faster when the target is huge and n is around 40.


What the problem actually asks

Subset sum asks whether some subset of n integers adds to a target T, or asks for the largest sum that does not exceed T. The knapsack dynamic program runs in time proportional to n times the target. A target with hundreds of bits makes that table impossible. Enumerating every subset is 2^n, which stops being reasonable a little past n = 30 on ordinary machines.

Splitting the instance in half turns one exponential into two square roots. The left subsets and the right subsets can be listed completely. A sum that uses items from both sides is a pair. Searching the pair space explicitly would square the lists. Sorting one list and probing it turns the pair test into a binary search.


What the algorithm guarantees

The search returns the exact optimum of this subset-sum instance: the largest subset sum that is at most T, or a report that none exists if that is the question. Every subset is a left subset plus a right subset, including the empty halves, so no combination is skipped. Time is dominated by building and sorting two lists of size 2^{n/2}, which is O(n · 2^{n/2}) arithmetic on integers of the input size. Extra memory is one of those lists.

A 0-1 knapsack with a separate value on each item can use the same split if each half stores pairs of weight and value and discards dominated pairs. The list can then be shorter than 2^{n/2}, and it is never longer. The plain sum problem does not need that filter. Negative numbers are allowed if the goal is restated. The usual statement uses positive integers and a sum cap.


The working steps

  1. Split the n items into two sets as equal as possible.
  2. Enumerate every subset sum of the left set and every subset sum of the right set. Include 0.
  3. Sort the right-hand sums.
  4. For each left sum a that is at most T, binary search the largest right sum that is at most T − a. Track the best total.
  5. A decision question stops early when a total equals T. An optimization question finishes the scan.

The exponential is 2^{n/2}, which is still exponential. A target T that fits in a table is the 0-1 knapsack recurrence, linear in nT and polynomial in the numeric value. Meet-in-the-middle is the exact algorithm for a moderate number of items and a target too large to tabulate.


Approaches that look simpler and fail

Approach Why it fails when both halves are enumerated and then nested naively
Pair every left sum with every right sum in a double loop The answer is correct and the time returns to Θ(2^n). The sort and the binary search are the whole saving.
Drop the empty subset A solution that lies entirely in one half uses the empty sum on the other side. Leaving out 0 misses it.
Replace the split with the knapsack table when T has hundreds of bits The table has a column per unit of capacity. It cannot be allocated. The split depends on n, not on T.
Keep only the sums that equal T in each half A feasible pair can be 40 plus T minus 40, with neither half equal to T. Every sum up to T has to stay in the lists.

A capacity table is 0-1 knapsack. A greedy density scan is fractional knapsack and allows cutting items. Meet-in-the-middle is the exact half-enumeration for subset sum.


A working example

This Python fragment splits the items in half, sorts one list of subset sums, and binary-searches the complement. The target 10 is reachable, so it prints yes.

import bisect
nums = [3, 5, 7, 9, 1, 2]
target = 10
mid = len(nums) // 2
left, right = nums[:mid], nums[mid:]

def sums(group):
    out = [0]
    for x in group:
        out += [s + x for s in out]
    return out

L, R = sums(left), sorted(sums(right))
found = False
for a in L:
    if a > target:
        continue
    j = bisect.bisect_right(R, target - a) - 1
    if j >= 0 and a + R[j] == target:
        found = True
assert found
print("yes" if found else "no")

Where it is used

Meet-in-the-middle splits a subset-sum or a knapsack of about forty items into two halves a computer can enumerate. The same split is why cascading a cipher twice does not double the key search. Bidirectional search is the graph form of the idea.


When you can drop this algorithm

Drop the split when nT fits in memory and time. The dynamic program is then faster and uses less memory. Keep the split when n is near 40 and the target is far larger than 2^{n/2}.


Why this stays published

We publish this so a search for meet-in-the-middle subset sum returns the two lists of size 2^{n/2} and the binary search that joins them. Meet-in-the-middle solves subset sum exactly by enumerating each half, sorting one list of sums, and binary searching the complement of every sum from the other half.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *