diff --git a/algorithms/CPlusPlus/Bit-Manipulation/print-all-subsets.cpp b/algorithms/CPlusPlus/Bit-Manipulation/print-all-subsets.cpp new file mode 100644 index 00000000..73e0e5d9 --- /dev/null +++ b/algorithms/CPlusPlus/Bit-Manipulation/print-all-subsets.cpp @@ -0,0 +1,56 @@ +/* +Description - To print all subsets/power set of an array using bit manipulation + +Approach - Iterate from 0 to (2^n)-1 + For each iteration, we will check the postion of set bits in its binary form + if i-th bit in that number is set,then we print i-th element in the array + +Time complexity - O(2^n) exponential +*/ + +#include +using namespace std; + +// Function to print the subsets +void printallsubsets(int* arr,int n){ + int total_length = 1<>1; //Bit right shift + p++; + } + cout<<"\n"; + + } + +} + +int main(){ + + int length_of_the_array = 3; + int arr[] = {1,2,3}; //Integer array is used + printallsubsets(arr,length_of_the_array); +} + +/* +Sample input- + array = {1,2,3} +Output- + + 1 + 2 + 12 + 3 + 13 + 23 + 123 + +*Note - one subset is empty subset +*/ + diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 2e2dc16d..28bde2b3 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -205,4 +205,8 @@ ## Backtracking -- [N-Queens Problem](Backtracking/n-queens.cpp) \ No newline at end of file +- [N-Queens Problem](Backtracking/n-queens.cpp) + +## Bit Manipulation + +- [Print all subsets of an array](Bit-Manipulation/print-all-subsets.cpp) \ No newline at end of file