Subsets II

Program for Printing all the Unique Subsets

You are given an array nums that contains integers, and some of these integers might be repeated. Your goal is to find all possible subsets (groups of numbers) that can be formed using the elements of this array.

A subset can include any combination of numbers, including no numbers at all (empty subset) or all the numbers in the array. However, your solution must ensure that no two subsets are the same. Even if the array contains duplicate numbers, the final list of subsets should not have duplicates.

You can arrange the subsets in any order when returning the result.

Printing Unique Subset

Constraints:

  • 1 <= nums.length <= 11
  • -20 <= nums[i] <= 20

Program for Combinational Sum

Recommendation for Time and Space Complexity – Try to solve the problem with a time complexity of O(n * 2^n) or better, and a space complexity of O(n), where n is the size of the input array.

Hints for solving problems

Hint 1 :

A basic solution would involve using a hash set to store all subsets and remove duplicates. After generating the subsets, you can convert the hash set into a list. However, this approach requires extra space of O(2^n). A better way is to sort the array first, as this helps identify and skip duplicate subsets during recursion.

Hint 2 :

You can use backtracking to generate all subsets. If the input array has duplicate numbers, sorting the array helps handle them efficiently. For instance, in the array [1, 1, 2], sorting lets you process the first 1 and skip the second 1 when building subsets. This ensures all subsets are unique.

Hint 3 :

Start by sorting the input array. Then, use a recursive function to explore subsets by including or excluding each number as you go from left to right. To avoid duplicates, skip numbers that are the same as the previous one. Once all recursive paths are processed, return the list of unique subsets.

There are mainly 4 approach to solve this problem-

  1. Brute Force Method
  2. Backtracking(Pick/Not Pick) Method
  3. Backtracking Method
  4. Iteration Method

1. Brute Force Method

In this approach, all possible subsets of the array are generated, including duplicates. A hash set is then used to store only unique subsets by filtering out duplicates. While simple to implement, this method is inefficient in terms of both time and space.

  • Time complexity: O(n* 2^n)
  • Space complexity: O(2^n)

Code

2. Backtracking(Pick/Not Pick) Method

This method uses recursion to explore all possible subsets by deciding at each step whether to include or exclude the current element. By sorting the input array and skipping duplicate elements, only unique subsets are generated efficiently.

  • Time complexity: O(n* 2^n)
  • Space complexity: O(n)

Code

3. Backtracking Method

This approach builds subsets recursively by extending the current subset one element at a time. Sorting the input array ensures that duplicate subsets are avoided by skipping consecutive duplicate elements during recursion.

  • Time complexity: O(n* 2^n)
  • Space complexity: O(n)

Code

4. Iteration Method

In this approach, subsets are built iteratively. Starting with an empty subset, for each element in the array, new subsets are created by adding the element to existing subsets. To handle duplicates, the input array is sorted, and new subsets are created only for elements that are not duplicates of the previous one.

  • Time complexity: O(n* 2^n)
  • Space complexity: O(1)

Code

More Articles