Meet in the Middle
O(2^(n/2))
Enumerate each half (2^(n/2) each), sort one, and binary-search to combine.
0%
mastery
0/5 solved
0/5 solved
Recognise it
The signal
n around 35-45: 2ⁿ is too big but 2^(n/2) fits; subset/closest sum with huge values; ~20x20 grid path with XOR.
1Brute force
Enumerate all 2n subsets.
2The observation
2n enumeration: each full solution is a pair of half-solutions, matched in sorted order.
3Why it works
Every full subset corresponds to exactly one (left half, right half) pair.
Reach for it when
- +subset sum / closest sum with huge values
- +path in grid of size ~20x20 with XOR
Not when
- ×Values are small enough for a sum-indexed tableuse Knapsack DP
- ×The combine step needs full state, not a pair of half-resultsuse No clean split; meet in the middle does not apply
Practice
0/5 solved
- Apple DivisionFoundationFoundationCSESEasy
- Meet in the MiddleApply it · Count subsets with sum x, n = 40.Apply itCSESMedium
- Closest Subsequence SumOne change · Closest instead of exact.One changeHard
- Partition Array Into Two Arrays to Minimize Sum DifferenceTwists · Exactly n elements per side.TwistsHard
- Xor-PathsMastery checkMastery checkCodeforces2100
Pitfalls
Classic bugs
- !Subset size constrained (exactly k items): group each half's sums by size and combine size i with size k-i.
Go deeper1 combinations · resources
Combinations
Meet in the Middle+Binary Search
n ~ 40 subset sum (exact, closest, count)
- Meet in the Middle:
- enumerates half-subsets
- Binary Search:
- matches each left sum to the best right sum
Closest Subsequence SumPartition Array Into Two Arrays to Minimize Sum DifferenceMeet in the Middle