I have an array, consisting of zeros and ones. For example, arr = [0,0,1,1,1]
How can I sort through all possible combinations, in which the number of ones and zeros remains constant (according to the example, the number of zeros is 2 and the number of ones is 3)? In total, there are C(5,3)
or C(5,2)
, where C = n! / ((n-k)! * k!))
.
I know how to get all possible combinations of ones and zeros in an array, there are 2 ^ n
pieces of them. There is code:
var color = [];
for(var i = 0; i < n;i++) color[i] = 0;
do{
color[0] += 1;
for(var i = 0; i < n-1;i++){
if(color[i] == 2) color[i] = 0, color[i+1] += 1;
else break;
}
if(color[n-1] == 2) break;
}
while(..);