Subsets

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write subsets(nums): nums holds 0 to 12 different whole numbers. Return an array of every subset of nums, each one an array, including the empty subset and nums itself, so n numbers have 2 to the power n subsets. The subsets can come in any order, and so can the numbers inside each one. Don't change nums. For example, subsets([1, 2, 3]) could return [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]. Each number is either in a subset or not, so decide for one number at a time.

Your code must pass

  • Lists every subset of [1, 2, 3]
  • Includes the empty subset
  • Lists all 4,096 subsets of 12 numbers
  • Leaves nums unchanged