Answer the question
In order to leave comments, you need to log in
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
The task is called "generation of combinations", and googles.
www.e-maxx-ru.1gb.ru/algo/generating_combinations
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question