Skip to content

Instantly share code, notes, and snippets.

@sreeprasad
Last active June 28, 2018 14:56
Show Gist options
  • Save sreeprasad/9e814cdc6eb3e2ee41ec27445d847c93 to your computer and use it in GitHub Desktop.
Save sreeprasad/9e814cdc6eb3e2ee41ec27445d847c93 to your computer and use it in GitHub Desktop.
all subsets of set
void generateSubset(char* array) {
int N = strlen(array);
int range = (1<<N)-1;
for (int i=0;i<range;i++) {
checkBitSetAndPrint(array, i);
}
}
void checkBitSetAndPrint(char* array, int N){
int i=0;
while (N>0) {
if (N&1) { // check if last bit is set
cout<<a[i]; // print ith index only if last bit is set
}
N=N>>1;
i++;
}
cout<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment