Meet in the Middle

Splitting search to reduce complexity.

Meet in the middle: split problem, solve halves, combine.

Subset sum: Find if any subset sums to target.

Brute force: 2n2^n subsets. n=401012n=40 \Rightarrow 10^{12}. Too slow.

Meet in middle: 1.1. Split into halves of 2020.

2.2. Generate 2202^{20} sums each.

3.3. For sum ss in first, check if targetstarget-s in second (hash set).

Time: O(2n/2n)O(2^{n/2} \cdot n). For n=40n=40: 4×107\sim 4 \times 10^7. Feasible.