Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which is the best way to generate choices out of a given set of numbers?

Tags:

c++

c

algorithm

for example if it is given to make all the choices between 1 to 5 and the answer goes like this..

1,2,3,4,5,  
1-2,1-3,1-4,1-5,2-3,2-4,2-5,3-4,3-5,4-5,  
1-2-3,1-2-4,1-2-5,1-3-4,
.....,
1-2-3-4-5.

can anyone suggest a fast algorithm?

like image 519
Vaibhav Avatar asked Nov 30 '22 10:11

Vaibhav


1 Answers

Just generate all the integers from one (or zero if you want to include the empty set) to 2^N - 1. Your sets are indicated by the set bits in the number. For example if you had 5 elements {A,B,C,D,E} the number 6 = 00110 would represent the subset {C,D}.

like image 138
deinst Avatar answered Dec 09 '22 21:12

deinst