Solve this problem using Permutations and Combinations Theory where I have to count number of possible arrangements

combinatoricspermutations

This was a mathematics question asked in a coding competition.

Given two numbers, $n,m$ count the number of sequences of $n$ length positive integers smaller or equal to $m$, such that no three consecutive numbers in the sequence are equal.

For eg: for Given $n=3, m=4$ the number of possible arrangements is $60$.
The total number of possible arrangements is $4\cdot4\cdot4\cdot4=64$.
The number of arrangements with any three consecutive elements repeating is $4$.
so answer is $64 – 4=60$

How can I find the answer for general values of $n,m$?

Best Answer

I'll just consider the case $n=6, m=3$ as mentioned in the OP's comment.

There are $3^6=729$ possible sequences. We want to subtract the number that have $3$ consecutive equal numbers.

There are $4$ positions in which such a sequence can start, and $3$ possible values. The remaining $3$ positions can be filled in $3^3$ ways, giving $12\cdot27=324$.

A sequence with four consecutive equal numbers will have been counted twice, so we must subtract $3\cdot3\cdot3^2=81$.

A sequence with $5$ consecutive equal numbers will have been added in $3$ times and subtracted twice, so it has been counted once, and no adjustment is needed.

A sequence with $6$ consecutive equal numbers has been added in $4$ times and subtracted $3$ times, so again, so adjustment is needed.

Now we have to consider sequences of the form $aaabbb$ with $a\neq b$. These have been added in twice, so we have to subtract them. There are $3$ ways to choose $a$ and then $2$ ways to choose $b$ so there are $6$ such sequences.

All in all, we have $$ 729-(324-81-6)=492$$

EDIT

Actually, it is possible to solve the general case, though it gets messy. Instead of using inclusion and exclusion, we set up a difference equation.

Call a sequence "admissible" if it doesn't contain $3$ consecutive equal characters. Let $a_n$ be the number of $n$-character admissible sequences. Let $b_n$ be the number of $n$-character admissible sequences whose last two characters are different, and let $c_n$ be the number of $n$-character admissible sequences whose last two characters are the same.

We have $$\begin{align} a_n&=b_n+c_n\tag1\\ b_{n+1}&=(m-1)b_n+(m-1)c_n\tag2\\ c_{n+1}&=b_n\tag3 \end{align}$$

$(2)$ is true because we get to an $n+1$-length sequence whose last characters are different by appending any character different from the last to any admissible sequence of length $n$.

$(3)$ is true because to get an admissible sequence whose last two characters are the same, we must duplicate the last character of any admissible sequence whose last two characters are different.

Combining $(2)$ and $(3)$ gives $$b_{n+1}=(m-1)b_n+(m-1)b_{n-1},\tag4$$ a linear, homogeneous, second-order difference equation with constant coefficients. We have the initial values, $b_0=0,\ b_1=m$, and $(4)$ can be solved by standard methods. (It may seem that we should have $b_0=1$, but we need $b_0=0$ in order that $(4)$ give $b_2=m(m-1).$

Once we have solved for $b_n$, we can rewrite $(1)$ and $(2)$ as $$a_n=\frac{b_{n+1}}{m-1}$$

I leave it to you to solve $(4)$.