![]() |
VOOZH | about |
Given an array arr[] of positive integers, Find all the unique subsets of the array.
A subset is any selection of elements from an array, where the order does not matter, and no element appears more than once. It can include any number of elements, from none (the empty subset) to all the elements of the array.
Note: If the array contains duplicate elements, the same subset should not appear more than once in the output. Only unique subsets should be included.
Examples:
Input: arr[] = [1, 5, 6]
Output: [[],[1], [1, 5], [1, 6], [5], [5, 6], [6], [1, 5, 6]]
Explanation: The Unique subset are [],[1], [1, 5], [1, 6], [5], [5, 6], [6], and [1, 5, 6]Input: arr[] = [1, 2, 2]
Output: [[],[1], [1, 2], [1, 2, 2], [2], [2, 2]]
Explanation: The Unique subset are [],[1], [1, 2], [1, 2, 2], [2], [2, 2]
Table of Content
The idea is to use backtracking to explore all possible subsets of the array. We first sort the array so that duplicate elements appear together. This allows us to easily skip over duplicates at the same recursion level and avoid generating the same subset more than once.
At each step, we add the current subset to the result, then try including each remaining element one by one. For every element, we choose it, recurse to explore further subsets, and then remove it (backtrack) to explore other possibilities. By repeating this process, we generate all unique subsets of the array.
[] [1] [1, 2] [1, 2, 2] [2] [2, 2]
Time Complexity: O(n * (2n)), as there are 2^n subsets, and pushing each subset (of size up to n) into the result takes O(n) time
Auxiliary Space: O(n), as recursion stack space and temporary array to store result
The idea is to build all unique subsets iteratively. We start with the empty subset {}. Then, for each element in the sorted array, we create new subsets by adding that element to every subset we have generated so far. To handle duplicates, we store all subsets in a set, which automatically removes any repeated subsets. After processing all elements, the set contains all unique subsets
[] [1] [1, 2] [1, 2, 2] [2] [2, 2]
The idea is to make all subsets one by one while avoiding duplicates. First, sort the array so duplicate numbers are together. Start with an empty subset, and for each number, add it to the existing subsets. If the number is a duplicate, only add it to the subsets created in the previous step to prevent repeated subsets.
[] [1] [2] [1, 2] [2, 2] [1, 2, 2]
Prerequisite: Power Set
Represent all the numbers from 1 to 2n- 1 where n is the size of the subset in the binary format and the position for which the bits are set to be added to the array and then add that array into the result i.e to use a bit-mask pattern to generate all the combinations.
Example:
Input: arr[] = [1, 5, 6], N = 3
Explanation: Hence we will check every number from 1 to 7 i.e. till 2n-1 = 23 - 1 = 7 and empty subset.
1 => 001 => [6]
2 => 010 => [5]
3 => 011 => [5, 6]
4 => 100 => [1]
5 => 101 => [1, 6]
6 => 110 => [1, 5]
7 => 111 => [1, 5, 6]
[] [1] [1, 2] [1, 2, 2] [2] [2, 2]