Skip to content

Instantly share code, notes, and snippets.

@alguerocode
Created June 7, 2022 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alguerocode/a728e4f11365ea3bfb1eef5aa9d796cf to your computer and use it in GitHub Desktop.
Save alguerocode/a728e4f11365ea3bfb1eef5aa9d796cf to your computer and use it in GitHub Desktop.
a function that generate and return count of how many subsequences of an array
#include <iostream>
#include <math.h>
int subsequence (int arr[],int length)
{
int counter = 0;
// how many times iterate (extend from this formula => 2^n - 1)
int power = pow(2, length);
for(size_t i = 1; i < power; i++)
{
std::cout << '[';
for(size_t j = 0; j < length; j++)
{
if(i & ( 1 << j))
{
std::cout << arr[j] << ',';
}
}
std::cout << ']' << std::endl;
}
return power - 1;
}
int main()
{
int arr[] = { 1, 2, 3, 4};
int length = sizeof(arr) / sizeof(*arr);
std::cout << subsequence(arr, length);
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment