Menu
CoddyTech

Subsets

You get a list nums of different integers. Return every subset of it, the empty one and the full list included, so n values give 2^n subsets. Write each subset with its values in ascending order, and list the subsets in lexicographic order: compare two subsets value by value, the first difference decides, and a subset that is the start of another comes before it. For [1, 2] the answer is [[], [1], [1, 2], [2]].

Function

subsets(nums: integer-array) → integer-2d-array
numsinteger-array
the values, all different, in any order
Returnsinteger-2d-array
every subset, each sorted ascending, listed in lexicographic order

Constraints

  • 1 ≤ nums.length ≤ 10
  • -10 ≤ nums[i] ≤ 10
  • All values in nums are different.
  • nums may come in any order.

Examples

Input
nums = [3, 1, 2]
Output
[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
Explanation
Sorted, the values are 1, 2, 3, and three values give 2^3 = 8 subsets. [1, 2] comes before [1, 2, 3] because it is its start, and [1, 2, 3] comes before [1, 3] because 2 is smaller than 3 at the second position.

lock icon+13 hidden tests on Submit

challenge icon

Follow-up

Can you produce the same list without recursion, building each subset directly from the one before it?

Reset code
def subsets(nums):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

nums = [3, 1, 2]

Expected

[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]