LeetCode 78 Subsets - Solution

The core idea

Use backtracking. For each element, decide whether to include it or not.

Start with an empty subset. At index ii, either add nums[i] and recurse, or skip it and recurse. When you reach the end of the array, add the current subset to results.

Alternatively: iterate through elements. At each step, for every existing subset, create a new subset by adding the current element.

Both approaches generate all 2n2^n subsets.