-1

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]);
                            }
                        }
                    }
                }
            }
        }
    }
}
}
Sundaze123
  • 23
  • 5
  • 5
    *I've been told that...* - have you considered asking the person that told you this? – achAmháin Jul 29 '18 at 22:30
  • What is the code even *supposed* to be doing? --- Also, the question is unclear. *"I don't seem to get how we reached that conclusion"* Which conclusion? That it produces 924 lines of output? – Andreas Jul 29 '18 at 22:46

1 Answers1

0

You're answering the combinatorial question how much "12 choose 6" is. By definition, the following holds:

12 choose 6 = 12!/6!(12-6)! 
            = 12!/(6!6!)     
            = 12*11*10...*2*1/(6*5*4*3*2*1*6*5*4*3*2*1) 
            = 479001600/(720*720) 
            = 924
user835611
  • 2,309
  • 2
  • 21
  • 29