With the upcoming advent of code I was looking at past problems and noted that one was easily solved with an algorithm such that for a length $n$ array, the algorithm finishes as soon as you encounter the second of two numbers that are randomly distributed within it.
I was trying to figure out the expected value for the number of items you need to look at out of $n$ randomly distributed items before finding two values of interest, but my probability is rusty.
I think I could finish the problem if I could nail down the combinatorics, but my combinatorics is too rusty.
There are clearly $n!$ ways to arrange $n$ items. The number of ways for the two items of interest to be in the first two items you look at is clearly $2$, so the expected value is $2\cdot 2/n!+3\cdot a_3/n!+\cdots +n\cdot a_{n}/n!$
I think if I could figure out the number of ways to arrange the 2 different items in a line of length $k$, then $a_k$ should be that number minus all the ways for $k-1$.
Am I okay so far? Can you help me finish the combinatorics solution?
I'm open to other smarter ways to solve the problem too but I'd like in particular to figure out the combinatorics.
What's the expected value?
Define $P(k)$ to be the probability that it takes $k$ searches to find both items of interest. There are $n!$ ways to arrange the $n$ objects. You will find $A$ and $B$ in exactly $k$ searches if one of $A$ or $B$ is the $k^\text{th}$ object you search and the other is searched before. There are $2$ ways to choose which special item is the $k^\text{th}$ object searched, and there are $k-1$ ways to choose where to put the other special item. There are $(n-2)!$ ways to arrange the other items.
Hence, $P(k)=\frac{2(n-2)!(k-1)}{n!}=\frac{2(k-1)}{n(n-1)}$. The expected value is $$\sum_{k=1}^n P(k)k$$ $$=\sum_{k=1}^n \frac{2k(k-1)}{n(n-1)}$$ $$=\frac{4}{n(n-1)}\sum_{k=2}^n \frac{k(k-1)}{2}$$ $$=\frac{4}{n(n-1)}\sum_{k=2}^n \binom{k}{2}$$ $$=\frac{4}{n(n-1)}\binom{n+1}{3}$$ $$=\frac{2(n+1)}{3}$$