Problem: Given 6 < k < 13 integers, enumerate all possible subsets of size 6 of these integers in a sorted order. Since the size of the required subset is always 6 and the output has to be sorted lexicographically (the input is already sorted), the easiest solution is to use 6 nested loops as shown below.
I read that this code is efficient because it'll only produces 924 lines of output for the largest test-case when k = 12; however, I don't seem to get how the 924 lines were counted. (Code solved using Iterative Complete Search problem solving paradigm)
public class loops {
public static void main(String[] args) {
int k = 12;
int[] S = {1,2,3,4,5,6,7,8,9,10,11,12};
for (int i = 0; i < k; i++) {
for (int a = 0; a < k - 5; a++) {
for (int b = a + 1; b < k - 4; b++) {
for (int c = b + 1; c < k - 3; c++) {
for (int d = c + 1; d < k - 2; d++) {
for (int e = d + 1; e < k - 1; e++) {
for (int f = e + 1; f < k; f++) {
System.out.println( S[a] + ""+ S[b] + S[c] + S[d] + S[e] + S[f]);
}
}
}
}
}
}
}
}
}