I was asked to implement the following function:
void printNumber(int N, int K);
It prints all binary numbers of length N
, which contains K
ones.
e.g.
input: printNumber(3,2)
output:
011
101
110
I tried to solve this problem by manipulating the binary as string and used recursion, but I guess there are some bit operation trick that could solve this problem in a nicer way.
Any bit-magic I can apply here?