Skip to content

Commit 31e8221

Browse files
authored
Merge pull request kelvins#325 from Hardvan/subsets-bit-manipulation
Add Subsets using Bit Manipulation
2 parents c8517d5 + 531f9de commit 31e8221

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3608,6 +3608,64 @@ In order to achieve greater coverage and encourage more people to contribute to
36083608
</a>
36093609
</td>
36103610
</tr>
3611+
<tr>
3612+
<td>Subsets Bit Manipulation</td>
3613+
<td> <!-- C -->
3614+
<a href="./CONTRIBUTING.md">
3615+
<img align="center" height="25" src="./logos/github.svg" />
3616+
</a>
3617+
</td>
3618+
<td> <!-- C++ -->
3619+
<a href="./src/cpp/SubsetsBitManipulation.cpp">
3620+
<img align="center" height="25" src="./logos/cplusplus.svg" />
3621+
</a>
3622+
</td>
3623+
<td> <!-- Java -->
3624+
<a href="./CONTRIBUTING.md">
3625+
<img align="center" height="25" src="./logos/github.svg" />
3626+
</a>
3627+
</td>
3628+
<td> <!-- Python -->
3629+
<a href="./CONTRIBUTING.md">
3630+
<img align="center" height="25" src="./logos/github.svg" />
3631+
</a>
3632+
</td>
3633+
<td> <!-- Go -->
3634+
<a href="./CONTRIBUTING.md">
3635+
<img align="center" height="25" src="./logos/github.svg" />
3636+
</a>
3637+
</td>
3638+
<td> <!-- Ruby -->
3639+
<a href="./CONTRIBUTING.md">
3640+
<img align="center" height="25" src="./logos/github.svg" />
3641+
</a>
3642+
</td>
3643+
<td> <!-- JavaScript -->
3644+
<a href="./CONTRIBUTING.md">
3645+
<img align="center" height="25" src="./logos/github.svg" />
3646+
</a>
3647+
</td>
3648+
<td> <!-- Swift -->
3649+
<a href="./CONTRIBUTING.md">
3650+
<img align="center" height="25" src="./logos/github.svg" />
3651+
</a>
3652+
</td>
3653+
<td> <!-- Rust -->
3654+
<a href="./CONTRIBUTING.md">
3655+
<img align="center" height="25" src="./logos/github.svg" />
3656+
</a>
3657+
</td>
3658+
<td> <!-- Scala -->
3659+
<a href="./CONTRIBUTING.md">
3660+
<img align="center" height="25" src="./logos/github.svg" />
3661+
</a>
3662+
</td>
3663+
<td> <!-- Kotlin -->
3664+
<a href="./CONTRIBUTING.md">
3665+
<img align="center" height="25" src="./logos/github.svg" />
3666+
</a>
3667+
</td>
3668+
</tr>
36113669
<tr>
36123670
<td>Two-Sum Problem</td>
36133671
<td> <!-- C -->

src/cpp/SubsetsBitManipulation.cpp

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

0 commit comments

Comments
 (0)