Given a integer N greater than zero.
How many sequences of 1's and 2's are there such that sum of the numbers in the sequence = N ?
(not necessary that every sequence must contain both 1 and 2 )
example :
for N = 2 ; 11,2 => ans = 2 sequences of 1's and 2's
for N = 3 ; 111,12,21 => ans = 3 sequences of 1's and 2's
I approached in the following two ways :
- Using recurrence relation (dynamic programming) : It is coming out to be same as Fibonacci relation with F(1) = 1 ( N=1 and we have only one possibility 11 ) and F(2) = 2 ( N=2 and we have two possibility 11 and 2 )
- Using domino tiling examples as described in the concrete mathematics book chapter : "Generating Function".
But I would like to know, is there any other way to solve this problem ? With possible idea and references .
If $N$ is even, then each sequence is made of $\color\red{2n}$ ones and $\color\green{N/2-n}$ twos.
Hence the total number of sequences is:
$$\sum\limits_{n=0}^{N/2}\frac{(\color\red{2n}+\color\green{N/2-n})!}{(\color\red{2n})!\times(\color\green{N/2-n})!}$$
If $N$ is even, then each sequence is made of $\color\red{2n+1}$ ones and $\color\green{N/2-n-1/2}$ twos.
Hence the total number of sequences is:
$$\sum\limits_{n=0}^{N/2}\frac{(\color\red{2n+1}+\color\green{N/2-n-1/2})!}{(\color\red{2n+1})!\times(\color\green{N/2-n-1/2})!}$$