# Analysis of longest run of heads when flipping a fair coin.

Consider a sequence of $$n$$ unbiased coin flips. Consider the length of the longest contiguous sequence of heads.

(a) Show that you are unlikely to see a sequence of length $$c + \log_2(n)$$ for $$c > 1$$ (give a decreasing bound as a function of c).

(b) Show that with high probability you will see a sequence of length $$\log_2 n − O(\log_2 \log_2 n)$$.

For (a) I want to use Markov's inequality to get a bound. For that, I need to find $$\mathbb{E}[X]$$ where $$X$$ is the longest run of heads after $$n$$ flips. I'm not sure how to get $$\mathbb{E}[X]$$.

To obtain $$E[X]$$ is not easy.

A simpler way: let $$g(n,m)$$ be the number of coins sequences with length $$n$$ that have some subsequence of at least $$m$$ heads.

Then $$g(n,m) \le 2^{n-m} (n+1-m) \tag 1$$

This is obtained by fixing one of the $$(n+1-m)$$ possible positions of the $$m-$$run inside the $$n$$ coins, and noting that the other coins might take any value. This is an upper bound, because it counts more than once some possibilities.

Because the coins are fair, all occurences are equiprobable. Then the probability of having some subsequence of at least $$m$$ heads is bounded by

$$p \le \frac{2^{n-m} (n+1-m)}{2^n}=\frac{(n+1-m)}{2^m} \tag 2$$

Setting $$m=c+\log_2(n)$$

$$p \le \frac{1}{2^c}\left(1 - \frac{c-1+\log_2n}{n}\right) \tag3$$

Update: this could be obtained a little easier. Let $$A_k$$ be the event "there is a run of $$m$$ consecutive zeroes starting at position $$k$$ inside the $$n-$$ sequence". Then the probability that there is at least one such run is

\begin{align} p &=P(A_1 \cup A_2 \cup \cdots A_{n+1-m}) \\ & \le P(A_1) + P(A_2) + \cdots P(A_{n+1-m}) \\ & = (n+1-m) P(A_i) \\ & = (n+1-m) 2^{-m} \\ \end{align} which agrees with $$(2)$$.