Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find all possible binary representations given the number of 1s? [closed]

Tags:

pseudocode

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...

like image 904
lin Avatar asked Feb 23 '23 10:02

lin


1 Answers

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 :)

like image 164
John Humphreys Avatar answered May 16 '23 05:05

John Humphreys