Skip to content

Commit

Permalink
Add Subsets using Bit Manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardvan committed Oct 28, 2023
1 parent dce9bd9 commit 454f006
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/cpp/SubsetsBitManipulation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#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;
}

0 comments on commit 454f006

Please sign in to comment.