# Expected value of the longest run of heads or tails in N flips of a coin

What is the expected value of the longest run of heads or tails observed in $$N$$ flips of a fair coin?

I'd like to consider this for any $$N$$, small or large.

Following the approach suggested in What is the expected length of the largest run of heads if we make 1,000 flips?, we expect $$N_k=2n(1-p)p^k=n\left(\frac12\right)^k$$ runs of heads or tails of length $$\ge k$$ (twice as many as if we only counted runs of heads). So a rough estimate of the expected maximum length is the solution for $$k$$ of $$N_k=n\left(\frac12\right)^k=1$$, which is $$k=\log_2n$$. Thus, the expected length is roughly the same as if we only count runs of heads, just $$\log_22=1$$ more.
Here's code that simulates this difference. For $$n=1000$$, the difference is about $$0.998$$, in good agreement with the approximation. Thus, you can basically use the answers to the above question and add $$1$$.