private static void CombinationUtil(IReadOnlyList <int> arr, List <List <int> >[] combinationList, int [] data, int start, int end, int index, int r, int pos, List <int> auxList, int[,] matrix) { List <int> auxList2 = new List <int>(); // Current combination is // ready to be printed, // print it if (index == r) { for (int j = 0; j < r; j++) { auxList2.Add(data[j]); } var auxMatrix = CreateAuxMatrix(matrix, auxList2, pos); if (GridFiller.LineDone(auxMatrix, 0, pos, FileManager.rowList[pos])) { combinationList[pos].Add(auxList2); } return; } // replace index with all // possible elements. The // condition "end-i+1 >= // r-index" makes sure that // including one element // at index will make a // combination with remaining // elements at remaining positions for (int i = start; i <= end && end - i + 1 >= r - index; i++) { data[index] = arr[i]; CombinationUtil(arr, combinationList, data, i + 1, end, index + 1, r, pos, auxList, matrix); } }