Subsets

mediumBacktrackingbacktracking include/exclude each elementSolve on LeetCode#78

Part of Practice: The Big Three

Given an array of distinct integers, return every possible subset (the power set) — every combination of elements, from the empty set up to the full array. The output must not contain duplicate subsets, and subsets may be listed in any order.

The array holds at most 10 elements, which is the giveaway that an exponential-size answer (2^n subsets) is expected and fine.

Examples

Input:
nums = [1, 2, 3]
Output:
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

All 2³ = 8 combinations of the three elements, in any order.

Input:
nums = [0]
Output:
[[], [0]]

A one-element array has exactly two subsets: empty and the element itself.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.