Created
June 7, 2022 11:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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