I want to get all the possible subsets for a given array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I want to get all the possible subsets for a given array

N is length and 2^n is list

26th Jul 2019, 9:29 AM
Ricky Kolluri
Ricky Kolluri - avatar
2 Answers
+ 9
You can make use of binary numbers for it, try writing down binary numbers starting from 0 to 2^n-1 & use that sequence to form subsets. example: subsets for {a, b} : 0 -> 00 -> {} 1 -> 01 -> {b} 2 -> 10 -> {a} 2^2 -1=3 -> 11 -> {a, b} you can take 1 means elements present & 0 means element is not present.
26th Jul 2019, 11:16 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Super.... thanks a lot
26th Jul 2019, 11:39 AM
Ricky Kolluri
Ricky Kolluri - avatar