-
Notifications
You must be signed in to change notification settings - Fork 266
/
FindDistinctSubsets.cpp
58 lines (49 loc) · 1.18 KB
/
FindDistinctSubsets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
/*
Problem Statement:
Given an integer array nums of unique elements, return all possible
subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any
order.
*/
// Using Bit Manipulation
// 1) Traverse all subsets
// 2) Traverse all elements of nums
// 3) Check if jth bit is set in i
// It is like for 3 elements:
// 000, 001, 010, 011, 100, 101, 110, 111
// where 1 means that element is present in the subset
// Time: O(n * 2ⁿ)
// Space: O(1)
vector<vector<int>> subsets(vector<int> &nums) {
int n = nums.size();
int total = 1 << n; // 2ⁿ
vector<vector<int>> ans(total);
// Traverse all subsets
for (int i = 0; i < total; i++) // 2ⁿ
{
// Traverse elements of nums
for (int j = 0; j < n; j++) // n
{
// Check if jth bit is set in i
if ((1 << j) & i) {
ans[i].push_back(nums[j]);
}
}
}
return ans;
}
int main() {
vector<int> nums = {1, 2, 3};
vector<vector<int>> ans = subsets(nums);
cout << "Subsets are: \n";
for (auto v : ans) {
cout << "[ ";
for (auto i : v) {
cout << i << " ";
}
cout << "]\n";
}
return 0;
}