2. [20] How many sequences of integers $\left(a_{1}, \ldots, a_{7}\right)$ are there for which $-1 \leq a_{i} \leq 1$ for every $i$, and
$$
a_{1} a_{2}+a_{2} a_{3}+a_{3} a_{4}+a_{4} a_{5}+a_{5} a_{6}+a_{6} a_{7}=4 ?
$$
Answer: 38
For $i=1,2, \ldots, 6$, let $b_{i}=a_{i} a_{i+1}$. From the problem condition each of $b_{1}, b_{2}, \ldots, b_{6}$ can only be $-1,0$, or 1 . Since the sum of these six numbers is 4 , either there are five 1 s and a -1 or there are four 1 s and two 0s.
In the first case, there are 6 ways to choose $i$ such that $b_{i}=-1$. Once that is fixed, determining the value of $a_{1}$ (one of 1 and -1 ) will determine the value of all the remaining $a_{i}$ 's, so there are $6 \cdot 2=12$ possible ways in this case.
In the second case, since if one of $b_{2}, b_{3}, b_{4}, b_{5}$ is zero, then one of the adjacent term to this zero term must also be zero. Therefore the two zeroes must be next to each other or be $b_{1}$ and $b_{6}$.
If $b_{1}=b_{2}=0$, then $a_{2}$ must be zero. $a_{1}$ 's value doesn't matter, and $a_{3}, a_{4}, \ldots, a_{7}$ must have the same sign. The same goes for $b_{5}=b_{6}=0$, giving $3 \cdot 2 \cdot 2=12$ possibilities in these two cases.
If $b_{i}=b_{i+1}=0$ for $i=2,3,4$, then $a_{i+1}$ must be zero. Moreover, $a_{1}, a_{2}, \ldots, a_{i}$ must have the same sign, and so do $a_{i+2}, \ldots, a_{7}$. this gives $2 \cdot 2 \cdot 3=12$ possibilities in these three cases.
If $b_{1}=b_{6}=0$, then $a_{1}=a_{7}=0$. Also, $a_{2}, a_{3}, \ldots, a_{6}$ must have the same sign so there are 2 possibilities. Combining these cases gives $12+12+12+2=38$ possible sequences in total.