Dynamic Programming21 sections · 916 units
Open in Course

Subset Sum - Problem Statement

Entry problem

Given an array of nn integers and a target sum SS, determine if any subset of the array sums to exactly SS. For example, with array [3,1,4,2][3, 1, 4, 2] and target 55, the answer is yes because {3,2}\{3, 2\} and {1,4}\{1, 4\} both sum to 55. Constraints: n20n \leq 20. With nn this small, 2n12^n \approx 1 million subsets is feasible to enumerate.

This is the simplest bitmask problem: iterate all subsets, check their sums. Before reading on, try to write the brute-force solution. How would you represent each subset? How would you compute its sum?