Solution: Subsets

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.

Pattern: Subsets#

Problems such as this one, where we need to find all possible subsets of a given set, can be efficiently solved using the subsets pattern. This pattern involves generating all possible subsets of a given set by using binary representations of indices to represent which elements should be included in each subset. This approach allows us to solve a wide range of problems that involve generating all possible subsets of a set.

Solution#

The idea is to generate binary numbers from 00 to 2nums.length2^{nums.length}. The number of bits in the binary numbers will equal nums.length. These integers will be added to the subset whose corresponding bits are set to 11 in the binary number.

Note: The ordering of bits for picking integers from the set doesn’t matter. We can pick integers from left to right and produce the same output as picking integers from right to left.

Created with Fabric.js 3.6.6 The size of the given set is 3, so 2^3 = 8 subsets will be formed. The value of "i"will iterate from 0 to 7. The output set is currently empty. Size of integer set = 3 0 1 2 2 5 7 Output set

1 of 9

Created with Fabric.js 3.6.6 i: 0 Binary of i 0 0 0 0 1 2 2 5 7 Output set {} Binary of i 000 000 is the binary of "i", so no integer is picked from the given set.The empty subset is added to the output set.

2 of 9

Created with Fabric.js 3.6.6 i: 1 Binary of i 0 0 1 0 1 2 2 5 7 Output set {} {2} Binary of i 000 001 001 is the binary of "i". Since only the last bit is set to 1, we include the element atindex 0 from the original set in the current subset. {2} is added to the output set.

3 of 9

Created with Fabric.js 3.6.6 i: 2 Binary of i 0 1 0 0 1 2 2 5 7 Output set {} {2} {5} Binary of i 000 001 010 010 is the binary of "i", so the integer at the 1st index is picked from the given set.{5} is added to the output set.

4 of 9

Created with Fabric.js 3.6.6 i: 3 Binary of i 0 1 1 0 1 2 2 5 7 Output set {} {2} {5} {2, 5} Binary of i 000 001 010 011 011 is the binary of "i", so the integers at the 0th and 1st indexes are picked from the given set.{2, 5} is added to the output set.

5 of 9

Created with Fabric.js 3.6.6 i: 4 Binary of i 1 0 0 0 1 2 2 5 7 Output set {} {2} {5} {2, 5} {7} Binary of i 000 001 010 011 100 100 is the binary of "i", so the integers at the 2nd index are picked from the given set.{7} is added to the output set.

6 of 9

Created with Fabric.js 3.6.6 i: 5 Binary of i 1 0 1 0 1 2 2 5 7 Output set {} {2} {5} {2, 5} {7} {2, 7} Binary of i 000 001 010 011 100 101 101 is the binary of "i", so the integers at the 0th and 2nd indexes are picked from the given set.{2,7} is added to the output set.

7 of 9

Created with Fabric.js 3.6.6 i: 6 Binary of i 1 1 0 0 1 2 2 5 7 Output set {} {2} {5} {2, 5} {7} {2, 7} {5, 7} Binary of i 000 001 010 011 100 101 110 110 is the binary of "i", so the integers at the 1st and 2nd indexes are picked from the given set.{5, 7} is added to the output set.

8 of 9

Created with Fabric.js 3.6.6 i: 7 Binary of i 1 1 1 0 1 2 2 5 7 Output set {} {2} {5} {2, 5} {7} {2, 7} {5, 7} {2, 5, 7} Binary of i 000 001 010 011 100 101 110 111 111 is the binary of "i", so the integers at the 0th, 1st, and 2nd indexes are picked from the given set.{2,5,7} is added to the output set.

9 of 9

Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.

Step-by-step solution construction#

The first step in solving this problem is calculating the total subsets generated from the given numbers. The total number of possible subsets for a set of size nn is 2n2^n. We’ll use this formula to calculate the total subsets for our input numbers.

Subsets

Once we have the count of all possible subsets, we can generate each subset using a binary representation of the indices. We will initiate a loop from i = 0 to i = subsets_count - 1, where subsets_count represents the total subsets. In each loop iteration, we will form a subset using the value of i as a bitmask.

Using i as a bitmask enables us to represent which elements from the original set should be included in the current subset. The set bits in i correspond to the indices of the elements to choose from the original set. For example, consider a set [1, 2, 3] and i = 2. In binary representation, 22 is denoted as 010. Since the second bit is set to 11, our subset will include the second element from the original set, which is 22. Therefore, the subset will be [2]. Similarly, for i = 3, the binary representation is 011. In this case, the set bits in i correspond to the first and second elements from the original set, resulting in the subset [1, 2]. Within each iteration of the loop (from 00 to subsets_count - 1), we will perform the following steps:

  • We will initialize an empty set called subset to store the subset being constructed.

  • During each iteration, we will iterate over the set nums elements using the variable j, representing the indices of the elements in nums.

  • For each j, we will call the get_bit(i, j) function to determine if the jthj^{th} bit of i is set to 11.

  • If get_bit(i, j) returns 11, indicating that the jthj^{th} bit is set to 11, and nums[j] is not already present in subset, we will add nums[j] to subset.

  • Finally, we will add the set subset to subsets.

Once the loop completes, the list subsets will contain all the subsets, including the empty set. This approach allows us to generate all possible subsets of the given set by considering each possible combination of elements using the binary representation of the indices.

Subsets

Just the code#

Here’s the complete solution to this problem:

Subsets

Solution summary#

To recap, the solution to this problem can be divided into the following parts:

  1. Calculate the subsets count using the formula 2n2^n.
  2. Iterate from 00 to the subsets count.
  3. Generate subsets using bitwise operations.
  4. Add each generated subset to the list of subsets.
  5. Return the list of subsets.

Time complexity#

The time complexity of this solution is exponential, specifically O(2nn)O(2^n \cdot n), where nn represents the number of integers in the given array.

Space complexity#

The space complexity of this solution is O(n)O(n), where nn represents the number of integers in the given array. This analysis excludes the space utilized by the output array, subsets, and only considers the space used by the subset set.

Subsets

Letter Combinations of a Phone Number