CodePark/ problems / All Subsets
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Backtracking

All Subsets

easyabout 15 minutes in an interview

nums holds distinct integers. Return every way to pick some of them, from picking none to picking all. Each pick is called a subset. Only which numbers you picked matters, not their order, so [2, 5] and [5, 2] are one subset.

  • Return an array of arrays. It must include the empty pick [] and the pick of everything.
  • List each subset once only.
  • Order doesn't matter anywhere: not the order of the subsets, and not the order of numbers inside each one.
  • nums has 0 to 10 numbers, each between -10 and 10. It can be empty.
  • Don't change nums.
Input: nums = [2, 5, 9] Output: [[], [2], [2, 5], [2, 5, 9], [2, 9], [5], [5, 9], [9]]There are 3 numbers, and each is either in or out. So there are 2 × 2 × 2 = 8 subsets.
Input: nums = [] Output: [[]]The only subset of an empty array is the empty subset.