for (int i = 0; i < 2n; i++){
if (number_of_on_bits(i) != k)
continue;
for (int j = 0; j < k; j++)
next_combination[j] = ar[index_of_jth_on_bit(i, j)];
}
n is 9 in your example.
k is 5 in your example.
index_of_jth_on_bit() returns the position of the jth bit that's in the on state. For example,
index_of_jth_on_bit(1100101b, 0) = 0
index_of_jth_on_bit(1100101b, 1) = 2
index_of_jth_on_bit(1100101b, 2) = 5
index_of_jth_on_bit(1100101b, 3) = 6
index_of_jth_on_bit(1100101b, 4 and above) = undefined (only 4 bits are on)
I'll use "NcM" to mean "N choose M". Given a set of N elements, NcM is
the first element, appended to (N-1)c(M-1) from the 2nd-Nth elements
the second element appended to (N-1)c(M-1) from the 3rd-Nth elements
the 3rd element appended to (N-1)c(M-1) from the 4th-Nth elements
etc.