Counting instances of apples and bananas next to each other Let's say I have a apples and b bananas arranged in a line and I generate all possible permutations of that arrangement (# would be ((b+a) choose a) or ((b+a) choose b). 
For each arrangement, how do I count the number of times an apple is next to a banana or vice-versa in terms of general a and b. If the arrangement is abba, the number of times would be 2. If the arrangement is bab, again the number of times would be 2
Thanks so much for your help!
 A: Let $n=a+b$. For $i=2$ to $n$, we say that there is a transition at $i$ if the contents of position $i$ are different from the contents of position $i-1$. Choose a permutation "at random." Define the indicator random variables $X_i$ by $X_i=1$ if there is a transition at $i$, and $0$ otherwise. 
The probability there is an apple at $i-1$ is $\frac{a}{n}$. Given that there is an apple at $i-1$, the probability there is a banana at $i$ is $\frac{b}{n-1}$. So the probability of an apple-banana transition at $i$ is $\frac{ab}{n(n-1)}$. We get the same probability for a banana-apple transition. 
The number of $T$ of transitions is $\sum_2^n X_i$. By the linearity of expectation, we have
$$E(T)=E\left(\sum_2^n X_i\right)=\sum_2^n E(X_i).$$
It follows that the expected number of transitions is $\frac{2ab}{n}$. 
Multiply by the number $\binom{n}{a}$ of arrangements to get the total number of transitions over all arrangements. We can simplify a little, and get the more symmetric-looking
$$\frac{2(a+b-1)!}{(a-1)!(b-1)!}$$
for the total number of transitions.
Remark: There is undoubtedly a simpler way to get the above expression, but the above mean proof is the first thing that came to mind. Indicator random variables, and the linearity of expectation can be very useful tools. 
