Task in Combinatorics course:
$1 \leq k < n$. Prove that the number $k$ can be found $(n-k+3)2^{n-k-2}$ times in sums that add up to $n$.
Example: $n=4, k=2$.
$1+1+2$
$1+2+1$
$2+1+1$
$2+2$
These are the sums that add up to $4$ and the number $2$ can be found $5$ times.
Thus far, I have tried making sense of that formula. $n-k$ is the number or countables left if we have one $k$. $2^{n-1}$ is how many sums that add up to $n$ there are in total so in the second half of the formula it could be $2^{n-1}2^{k-1}$ that would add up to $2^{n-k-2}$. But none of that comes together. Why would there be a $+3$ in the first half.
A composition which sums to $n$ corresponds to the placement of either a comma or an addition sign in each of the $n - 1$ spaces between successive ones in a row of $n$ ones.
$$1 \square 1 \square 1 \square \cdots \square 1 \square 1 \square 1$$ Since there are $2$ ways to fill each of the $n - 1$ spaces, there are $2^{n - 1}$ compositions of the number $n$.
We are told that one of the summands is $k$. That means there is a block of $k$ consecutive ones. Such a block must begin in one of the first $n - k + 1$ positions. Of these, two are at the ends of the row and $n - k - 1$ include neither end of the row. We will consider cases, depending on the position of the block.
If the first summand is $k$, there is a block of exactly $k$ ones at the start of the row. That block must be immediately followed by an addition sign. That leaves $n - k$ ones and $n - k - 1$ spaces where either a comma or an addition sign can be placed. Hence, there are $2^{n - k - 1}$ such compositions.
If the last summand is $k$, there is a block of exactly $k$ ones is at the end of the row. That block must be immediately preceded by an addition sign. That leaves $n - k$ ones and $n - k - 1$ spaces where either a comma or an addition sign can be placed. Hence, there are $2^{n - k - 1}$ such compositions.
If the summand $k$ does not appear in the first or last position of the composition, there are $n - k - 1$ places where the block of exactly $k$ consecutive ones could begin. The block must be immediately preceded and immediately followed by addition signs. That leaves $n - k$ ones and $n - k - 2$ spaces where either a comma or an addition sign can be placed. Hence, there are $(n - k - 1)2^{n - k - 2}$ such compositions.
Since these cases are mutually exclusive and exhaustive, the number of times the summand $k$ appears in compositions of $n$ is \begin{align*} 2 \cdot 2^{n - k - 1} + (n - k - 1)2^{n - k - 2} & = 4 \cdot 2^{n - k - 2} + (n - k - 1)2^{n - k - 2}\\ & = (n - k + 3)2^{n - k - 2} \end{align*} Note that we have implicitly used the hypothesis that $k < n$ in order to separate the cases where $k$ is the first summand, $k$ is the last summand, and $k$ is neither the first nor last summand.