Advertisement
vencinachev

Combinations - generate

Oct 15th, 2023
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. static List<string[]> GenerateCombinations(string[] elements, int N)
  2.         {
  3.             List<string[]> combinations = new List<string[]>();
  4.             GenerateCombinationsRecursive(elements, N, 0, new string[N], combinations);
  5.             return combinations;
  6.         }
  7.  
  8.         static void GenerateCombinationsRecursive(string[] elements, int N, int startIndex, string[] currentCombination, List<string[]> combinations)
  9.         {
  10.             if (N == 0)
  11.             {
  12.                 combinations.Add((string[])currentCombination.Clone());
  13.                 return;
  14.             }
  15.  
  16.             for (int i = startIndex; i < elements.Length; i++)
  17.             {
  18.                 currentCombination[currentCombination.Length - N] = elements[i];
  19.                 GenerateCombinationsRecursive(elements, N - 1, i + 1, currentCombination, combinations);
  20.             }
  21.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement