Permutations of a binary strings with even number of 1's

335 Views Asked by At

So I'm a bit rusty, but given a binary string, I'd like to calculate the permutations that exist which contain an even number of 1's within the range of binary 0 to said binary string.

In other words, for all binary strings for the decimal range 0 - n, as n is the given upper bound. Below, I'll use 20 as an example.

For e.g

10100 (dec: 20), and starting from 0:

11 (dec: 3), 1010 (dec: 10) etc.

I figured the calculation would be:

nCr(5,2) + nCr(5,4) as the length is 5, and evens in that range are 2,4. This yields 15, though the answer should be 10 (I'm pretty sure)

What am I missing?

1

There are 1 best solutions below

2
On

What goes wrong with your method is that $5 \choose 2$ picks out all strings of length $5$ that have two $1$'s ... but that includes a string like $11000$ which is greater than $20$. Same for $5 \choose 4$: a string like $11011$ should not be included.