Given N as the number of bits and K as the Number of 1s, how can i generate all binary representations that contain K ones and N-k zeros?
in other words i have:
N=4 //number of bits
K=2 //number of ones
All possible binary values containing N bits,K ones and N-K zeros are:
1100
1010
1001
0110
0101
0011
i have nothing so far. i am not asking for code. i am just asking for the best way to do it? an algorithm? a pseudocode? maybe a discussion?
Edit: I am asking for code/pseudocode to solve the problem... and not a math formula...
Remember combinations and permutations from math class?
Google it and find the equation, use: http://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html for a quick calculation :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With