Sabino
Sabino

Reputation: 1

Java: Simple Combination of a set of element of k order

I have a set of numbers {'1','13','25','32','49',...}, i want to calculate all possible combinations of this numbers of order k.

Esample1:

set = {'1','5','23','41,'54','63'};
k = 4;

Output1:

1 5 23 41
1 5 23 54
1 5 23 63
1 5 41 54
1 5 41 63
1 5 54 63
1 23 41 54
1 23 41 63
1 23 54 63
1 41 54 63
5 23 41 54
5 23 41 63
5 23 54 63
5 41 54 63
23 41 54 63

Example2:

set = {'a','v','f','z'};
k=3;

Output2:

a v f
a v z
a f z
v f z

in Java plaese.

Thank you!

Upvotes: 0

Views: 2776

Answers (1)

milan
milan

Reputation: 12402

You should be able to find an appropriate algorithm in D.Knuth's The Art of Computer Programming, Volume 4, fascicle 3 - Generating All Combinations, which can be downloaded from his website.

Upvotes: 6

Related Questions