<s>[INST] Doubt in the solution of a competition combinatorial problem concerning the average number of times the letters of a sequence change I am currently going through  Andreescu's 102 combinatorial problems. I couldn't solve number 28 after repeated attempts, so I checked the solution. The problem and solution go as follows:

However, I do not understand why summing the $N_i$'s and dividing over $20!$ is the average. As I see it, if $S_i$ denotes the number of rows with $i$ boy-girl or girl-boy appearances in the row, the average should be
$\dfrac{\sum_{i=2}^{13}iS_i}{\binom{20}{7}}$.
I was hoping that someone would throw some light on why my formula is incorrect and the logic behind the solution (I am relatively new to combinatorial problems in competitions.)
 [/INST] Sometimes it is helpful to reduce the problem to smaller numbers so that it can be calculated manually. Here we take $5$ instead of $20$ persons and consider a group of two boys and three girls.
\begin{align*}
\{B_1,B_2,G_1,G_2,G_3\}\tag{1}
\end{align*}
In order to analyse the situation it is sufficient to consider the
\begin{align*}
\color{blue}{\binom{5}{2}=10}
\end{align*}
different rows
\begin{align*}
\begin{array}{l|c}
B\quad B\color{blue}{\bullet} G\quad G\quad G&1\\
B\color{blue}{\bullet} G\color{blue}{\bullet} B\color{blue}{\bullet} G\quad G&3\\
B\color{blue}{\bullet} G\quad G\color{blue}{\bullet} B\color{blue}{\bullet} G&3\\
B\color{blue}{\bullet} G\quad G\quad G\color{blue}{\bullet} B&2\\
G\color{blue}{\bullet} B\quad B\color{blue}{\bullet} G\quad G&2\\
G\color{blue}{\bullet} B\color{blue}{\bullet} G\color{blue}{\bullet} B\color{blue}{\bullet} G&4\\
G\color{blue}{\bullet} B\color{blue}{\bullet} G\quad G\color{blue}{\bullet} B&3\\
G\quad G\color{blue}{\bullet} B\quad B\color{blue}{\bullet} G&2\\
G\quad G\color{blue}{\bullet} B\color{blue}{\bullet} G\color{blue}{\bullet} B&3\\
G\quad G\quad G\color{blue}{\bullet} B\quad B&1
\end{array}
\end{align*}
Each of these rows represents the $2!\cdot 3!=12$ pairwise different configurations when we use the elements from (1) giving a total of $12\cdot 10=5!$ rows. But this is not so important, as we can only focus on $BG$ and $GB$ combinations which are marked with $\color{blue}{\mathrm{blue}}$ dots.
One approach:
We get the multiplicities from the right-most column of the table above and derive according to OPs approach
\begin{align*}
\color{blue}{\frac{1\cdot S_1+2\cdot S_2 + 3\cdot S_3+4\cdot S_4}{\binom{5}{2}}}=\frac{1\cdot 2+2\cdot 3+3\cdot 4+4\cdot 1}{10}
\color{blue}{=2,4}
\end{align*}
Another approach:
We focus on the combinations $BG$ and $GB$ for each of the four position pairs $(1,2), (2,3), (3,4), (4,5)$.
\begin{align*}
\begin{array}{ccccc}
B&G&\cdot&\cdot&\cdot\\
\cdot&B&G&\cdot&\cdot\\
\cdot&\cdot&B&G&\cdot\\
\cdot&\cdot&\cdot&B&G\\
G&B&\cdot&\cdot&\cdot\\
\cdot&G&B&\cdot&\cdot\\
\cdot&\cdot&G&B&\cdot\\
\cdot&\cdot&\cdot&G&B\\
\end{array}
\end{align*}
We fix a specific position $j, 1\leq j\leq 4$ and let's say the combination $BG$. We calculate the probability of the event that $BG$ occurs at a specific position $j$ by dividing the favorable number of possibilities by the total number which is as we have two boys and three girls:
\begin{align*}
\color{blue}{\frac{2\cdot 3}{5\cdot 4}}\tag{2}
\end{align*}
We note that (2) is strongly related with $N_j$ from OPs problem, since we can (2) write as
\begin{align*}
\frac{2\cdot 3}{5\cdot 4}=\frac{2\cdot 3\cdot 3!}{5\cdot 4\cdot 3!}=\frac{2\cdot 3\cdot 3!}{5!}\tag{3}
\end{align*}
Doubling (3) since we have to respect $BG$ as well as $GB$ at a specific position and using the linearity of expectation we calculate according to (2) and (3)
\begin{align*}
\color{blue}{\frac{N_1+N_2+N_3+N_4}{5!}}=4\cdot \frac{2 (2\cdot 3) 3!}{5!}=4\cdot\frac{ 2(2\cdot 3)}{5\cdot 4}\color{blue}{=2,4}
\end{align*}
and both results coincide.
</s>