/* C++ code to generate all possible subsequences.
Time Complexity O(n * 2^n) */
#include<bits/stdc++.h>
using namespace std;
void printSubsequences(int arr[], int n)
{
/* Number of subsequences is (2**n -1)*/
unsigned int opsize = pow(2, n);
/* Run from counter 000..1 to 111..1*/
for (int counter = 1; counter < opsize; counter++)
{
for (int j = 0; j < n; j++)
{
/* Check if jth bit in the counter is set
If set then print jth element from arr[] */
if (counter & (1<<j))
cout << arr[j] << " ";
}
cout << endl;
}
}
// Driver program
int main()
{
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "All Non-empty Subsequences
";
printSubsequences(arr, n);
return 0;
}
Code Example |
---|
Cpp :: c++ remove last element from array |
Cpp :: converter c++ to c |
Cpp :: main function |
Cpp :: how to make a segment tree in c++ |
Cpp :: how to write hello world c++ |
Cpp :: delete a head node in link list |
Cpp :: c++ permutation |
Cpp :: stream in c++ |
Cpp :: how to replace an element in array in c++ |
Cpp :: c++ return statement |
Cpp :: pointer to constant |
Cpp :: how to get part from the vector cpp |
Cpp :: how to implement binders and decorators on c++ lik python? |
C :: stop redis server |
C :: calculate distance between 2 points X Y axis |
C :: docker container give usb access |
C :: space after format specifiers in c |
C :: sdl draw Rectf |
C :: find power of a number in c |
C :: sigaction in c |
C :: input in c |
C :: exclamation mark in c |
C :: c/c++ type format |
C :: arduino millis |
C :: how do you make a copy of a linked list in c |
C :: slug urls django |
C :: c code to add two numbers |
C :: hello word in c |
C :: round float in c |
C :: int to double c |