I want an algorithm to compute all permutations of a fixed size binary number with a given hamming weight. For example, if the hamming weight is 2 and the binary size is 4 t
You can generate them in the following manner:
Initially, make a vector with n - r zeros in the beginning and r ones in the end(0011
for n = 4 and r = 2).
Then, repeat the following procedure:
0110
, we first move the rightmost one that can be moved to the left and get 1010
, then we shift all ones to the right from it to the end of the vector and get 1001
.This solution has O(C(n, r) * n)
time complexity. One more feature of this solution: it generates elements in lexicographical order.