M
M
mikhle192018-10-17 21:50:52
C++ / C#
mikhle19, 2018-10-17 21:50:52

How to iterate over all combinations of m elements from n objects, without repeating?

Good afternoon. The setting task is simple. There is an array of n elements ([1...9]), m is the number of elements in one combination. Main limitation: it is impossible for more than one element to be repeated in different combinations.
For example:
For array 1 2 3 5 4 6 7 8 9
You need to get the following combinations
1 2 3
4 5 6
7 8 9
1 4 7
1 5 8
1 6 9
2 4 8
2 5 9
2 6 7
3 4 9
3 5 7
3 6 8
All enumeration with verification will not work. And if you go in a normal cycle, it will lead to this solution:
1 2 3
1 4 5
1 6 7
1 8 9
2 4 6
2 5 7
2 8
2 9
3 4 7
3 5 8
3 9
etc.
Tell me, please, either some tips on the Internet, or what algorithms can help. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Olohtonov, 2018-10-17
@sgjurano

The task is called "generation of combinations", and googles.
www.e-maxx-ru.1gb.ru/algo/generating_combinations

D
Dmitry, 2018-10-18
@Trif

I once suggested one approach in an article on Habré ( however, in the comments I was later told that it was "search for permutations through the factorial number system and the Lehmer code" ).
Look, there is not a lot, maybe it will become clearer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question