Given a number **N**. The task is to find the expected number of times a coin must be flipped to get **N** heads consecutively.

**Example:**

Input:N = 2

Output:6

Input:N = 5

Output:62

**Approach:**

The key is to observe that if we see a tail between any consecutive **N** flip, it breaks the streak for continuous heads and we have to start over again for **N** consecutive head.

Let the expected number of trial be **X** to get **N** consecutive heads. Below are the possible Cases:

**Case 1:**If, in the**1st trial**, a tail occurs then it means that we have wasted one trial and we will have to do**X**more trial to get**N**consecutive head. The probability of this event is**1/2**and the total number of trial required to get**N**consecutive head is**(X + count of the previous trial wasted)**.**Case 2:**If, in the**2nd trial**, a tail occurs then it means that we have wasted our all previous trial and we will have to do**X**more trial to get**N**consecutive head. The probability of this event is**1/4**and the total number of trials required to get**N**consecutive flips is (X + count of previous trial wasted).**Case 3:**If, in the**3rd trial**, a tail occurs then it means that we have wasted our all previous trial and we will have to do**X**more trial to get**N**. The probability of this event is**1/8**and the total number of trials required to get**N**consecutive flips is**(X + count of the previous trial wasted)**. This will continue until we get**N**consecutive heads.**Case N:**Similarly, if in the**Nth trial**, a tail occurs, then it means that we have wasted our all previous trial and we will have to do**X**more trial to get**N**. The probability of this event is**1/2**and the total number of trials required to get^{N}**N**consecutive flips is**(X + count of the previous trial wasted)**.

From the above cases, the summation of all probability gives will gives the count of trials for **N consecutive heads**. Mathematically:

X = (1/2)*(X+1) + (1/4)*(X+2) + (1/8)*(X+3)+. . .+(1/2

^{N})*(X+N) + (1/2^{N})*N

Solving the above equation for X. We have:

By opening the above expressions and arranging it we have: X = X(1/2 + 1/4 + 1/8 + . . . . . . 1/2^{N}) + (1/2 + 2/4 + 3/8 . . . . . . . + N/2^{N}+ N/2^{N})

The first part of the above equations form Geometric Progression and second part of the above equations forms an Arithmetico Geometric Sequence. Solving the above sequences separately we have:

For **Geometric Sequence:**

Sum of GP series = 1/2 + 1/4 + 1/8 + . . . . . . 1/2

^{N}

first term(a) is 1/2

common ratio(r) is 1/2

last term (nth term) is 1/2^{N}which is also a * r^{N-1}

Hence sum is given by:

Sum of GP series = (1/2)*( (1 – (1/2)^{N})/(1 – 1/2) ) using formula : (a * (1 – r^{N})) / (1 – r) since r < 1

Sum of GP series = (1 – (1/2)^{N})

For **Arithmetico Geometric Sequence**:

Let S = Sum of Arithmetico Geometric Sequence:

=> S = (1/2 + 2/4 + 3/8 + . . . . . . N/2

^{N}) …….(1)Multiplying By 2, we get

=> 2S = (1 + 2/2 + 3/4 + . . . . . . . + N/2^{N-1}) …….(2)Subtracting the equation(1) from the equation(2), we get

=> S = (1/2 + 1/4 + 1/8 + . . . . . . 1/2^{N-1}) – N/2^{N}

=> S = sum of GP series – N/2^{N}

=> S = (2 – (1/2)^{N-1})) – N/2^{N}

Using the sum of the GP series and Arithmetico Geometric Sequence:

=> X = X*(1 – (1/2)

^{N}) + (2 – (1/2)^{N-1}) – N/2^{N}+ N/2^{N}

=> X = X*(1 – (1/2)^{N}) + (2 – (1/2)^{N-1})

=> X*((1/2)^{N}) = (2 – (1/2)^{N-1})

=>X = 2^{N+1}– 2

Now the above formula for **X** gives the number of trials requires getting **N consecutive heads**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include "bits/stdc++.h" ` `using` `namespace` `std; ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 3; ` ` ` ` ` `// Formula for number of trails for ` ` ` `// N consecutive heads ` ` ` `cout << ` `pow` `(2, N + 1) - 2; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG{ ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `3` `; ` ` ` ` ` `// Formula for number of trails for ` ` ` `// N consecutive heads ` ` ` `System.out.print(Math.pow(` `2` `, N + ` `1` `) - ` `2` `); ` `} ` `} ` ` ` `// This code is contributed ` `// by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `3` ` ` ` ` `# Formula for number of trails for ` ` ` `# N consecutive heads ` ` ` `print` `(` `pow` `(` `2` `, N ` `+` `1` `) ` `-` `2` `) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 3; ` ` ` ` ` `// Formula for number of trails for ` ` ` `// N consecutive heads ` ` ` `Console.Write(Math.Pow(2, N + 1) - 2); ` `} ` `} ` ` ` `// This code is contributed ` `// by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

14

**Time Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Expected Number of Trials until Success
- Expected number of coin flips to get two heads in a row?
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Probability of not getting two consecutive heads together in N tosses of coin
- Puzzle | Can 2 persons be with same number of hairs on their heads?
- Expected number of moves to reach the end of a board | Dynamic programming
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Probability of getting at least K heads in N tosses of Coins
- Puzzle 43 | Muddy Heads
- Puzzle | Heads or Tails
- Probability of getting K heads in N coin tosses
- Count of total Heads and Tails after N flips in a coin
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Check if a number can be expressed as a sum of consecutive numbers
- Check if a number can be written as sum of three consecutive integers
- Express a number as sum of consecutive numbers
- Count ways to express a number as sum of consecutive numbers
- Find the number of consecutive zero at the end after multiplying n numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.