Statement#

Given an array of integers, nums, find all possible subsets of nums, including the empty set.

Note: The solution set must not contain duplicate subsets. You can return the solution in any order.

Constraints:

  • 11 \leq nums.length 10\leq 10
  • 10-10 \leq nums[i] 10\leq 10
  • All the numbers of nums are unique.

Examples#

Created with Fabric.js 3.6.6 3 6 9 Input Sample example 1 [] [3] [6] [9] [3, 6] [3, 9] [6, 9] [3, 6, 9]

1 of 4

Created with Fabric.js 3.6.6 Input 2 4 Sample example 2 [] [2] [4] [2, 4]

2 of 4

Created with Fabric.js 3.6.6 Input 9 Sample example 3 [] [9]

3 of 4

Created with Fabric.js 3.6.6 Input [] [] Sample example 4

4 of 4

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Subsets

1

Find all subsets for the given set.

nums = [6, 7, 8]

A)

[[], [6], [7], [8], [6, 7], [6, 8], [7, 8]]

B)

[[], [6], [7], [8], [6, 7], [6, 8], [7, 8], [6, 7, 8]]

C)

[[], [6], [6, 7], [6, 8], [7, 8], [6, 7, 8]]

D)

[[6], [7], [8], [6, 7], [6, 8], [7, 8], [6, 7, 8]]

Question 1 of 40 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Compute the number of possible subsets of the given set using 2n2^n, where nn is the number of elements.

Start a loop from 00 to the count of subsets and add an empty list to the results list in the first iteration.

In each iteration, create a bit mask of length nn for each element in the input set. If the ithi^{th} bit is set, set[i]set[i] will be present in the current subset.

After iterating over all elements in the input set, append the current subset to the list of subsets.


Try it yourself#

Implement your solution in the following coding playground:

Python
usercode > main.py
Input #1
%0 node_01 1
Visualization for Input #1
Subsets

Solution: Permutations

Solution: Subsets