Premium problem96. Expected HH Pairs

Medium Locked

Flip a biased coin n times, heads with probability p. Return the expected number of adjacent positions where both flips were heads.

There are n−1n-1 adjacent pairs and each is HH with probability p2p^2, so the answer is (n−1)p2(n-1)p^2. The pairs overlap and are therefore dependent, which does not matter: expectation adds regardless.

Input

n = 10
p = 0.5

Output

2.25

Premium problem

This one's part of Premium. Unlock the full Probability track plus every other premium problem on the site.

Implement solve(...)