Given that a coin is tossed N times. The task is to find the total number of the sequence of tosses such that after the first head from left, all the alternating positions to the right of it are occupied by the head only. The positions except the alternating position can be occupied by any of head or tail.

For example, if you are tossing a coin 10 times (N = 10) and the first head occurs at the 3rd position, then all the further alternating positions to the right are 5, 7, 9…

**Examples:**

Input:N = 2Output:4

All possible combinations will be TT, TH, HT, HH.

Input:N = 3Output:6

All possible combinations will be TTT, TTH, THT, THH, HTH, HHH.

In this case, HHT & HTT is not possible because in this combination

the condition of alternate heads does not satisfy. Hence, the answer will be 6.

**Approach:**

If the sequence is to start with a tail, then all possible sequences of length N-1 are valid. Now, if the sequence is to start with a head, then every odd index in the sequence (assuming the sequence is 1-based) will be head, and the rest of the N/2 places can be either head or tail. Thus, the recursive formula for the above problem will be:

f(N) = f(N-1) + 2floor(N/2)

**Mathematical Calculation:**

Letfand_{o(N)}fbe the functions that are defined for the_{e(N)}oddandevenvalues of N respectively. f_{o}(N) = f_{e}(N-1) + 2^{(N-1)/2}f_{e}(N) = f_{o}(N-1) + 2^{N/2}From above equation compute f_{o}(N) = f_{o}(N-2) + 2^{(N-1)/2}+ 2^{(N-1)/2}f_{e}(N) = f_{e}(N-2) + 2^{N/2}+ 2^{(N-2)/2}Base Case: f_{o}(1) = 2 f_{e}(0) = 1 By using the above equation, compute the following results : f_{o}(N) - f_{o}(N-2) = 2^{(N-1)/2}+ 2^{(N-1)/2}f_{o}(N) - f_{o}(N-2) = 2^{(N+1)/2}By taking the sum of above equation for all odd values of N, below thing is computed. f_{o}(N) - f_{o}(N-2) + f_{o}(N-1) - f_{o}(N-3) + ...... + f_{o}(3) - f_{o}(1) = 2^{2}+ 2^{3}+ 2^{4}+ ..... + 2^{(N+1)/2}Hence on summation, f_{o}(N) - f_{o}(1) = SUM_{[ n = 0 to (N+1)/2 ]}2^{n}- 2^{1}- 2^{0}By using sum of geometric progressionfSimilarly, find f_{o(N) = 2( N + 1 ) / 2 + 2( N + 1 ) / 2 - 2}_{e}(N) : f_{e}(N) = f_{e}(N-2) + 2^{N/2}+ 2^{(N-2)/2}f_{e}(N) - f_{e}(0) = SUM_{[ n = 0 to N/2 ]}2^{n}- 2^{0}- SUM_{[ n = 0 to (N-1)/2 ] }2^{n}By using sum of geometric progressionf_{e(N) = 2 (N / 2) + 1 + 2 N / 2 - 2}

The final formula will be as follows:

f(N) = 2, if N is odd^{(N+1)/2}+ 2^{(N+1)/2}– 2f(N) = 2, if N is even^{(N/2) + 1 }+ 2^{N/2}– 2

Below is the implementation of the above approach:

## C++

`// C++ program to find number of sequences` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// function to calculate total sequences possible` `int` `findAllSequence(` `int` `N)` `{` ` ` `// Value of N is even` ` ` `if` `(N % 2 == 0) {` ` ` `return` `pow` `(2, N / 2 + 1) + ` `pow` `(2, N / 2) - 2;` ` ` `}` ` ` `// Value of N is odd` ` ` `else` `{` ` ` `return` `pow` `(2, (N + 1) / 2) + ` `pow` `(2, (N + 1) / 2) - 2;` ` ` `}` `}` `// Driver code` `int` `main()` `{` ` ` `int` `N = 2;` ` ` `cout << findAllSequence(N) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java program to find` `// number of sequences` `import` `java.io.*;` `class` `GFG` `{` `// function to calculate` `// total sequences possible` `static` `int` `findAllSequence(` `int` `N)` `{` ` ` `// Value of N is even` ` ` `if` `(N % ` `2` `== ` `0` `)` ` ` `{` ` ` `return` `(` `int` `)(Math.pow(` `2` `, N / ` `2` `+ ` `1` `) +` ` ` `Math.pow(` `2` `, N / ` `2` `) - ` `2` `);` ` ` `}` ` ` ` ` `// Value of N is odd` ` ` `else` ` ` `{` ` ` `return` `(` `int` `)(Math.pow(` `2` `, (N + ` `1` `) / ` `2` `) +` ` ` `Math.pow(` `2` `, (N + ` `1` `) / ` `2` `) - ` `2` `);` ` ` `}` `}` `// Driver code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `N = ` `2` `;` ` ` `System.out.print( findAllSequence(N));` `}` `}` `// This code is contributed` `// by anuj_67.` |

## Python3

`# Python3 program to find number` `# of sequences` `# function to calculate total` `# sequences possible` `def` `findAllSequence(N):` ` ` `# Value of N is even` ` ` `if` `(N ` `%` `2` `=` `=` `0` `):` ` ` `return` `(` `pow` `(` `2` `, N ` `/` `2` `+` `1` `) ` `+` ` ` `pow` `(` `2` `, N ` `/` `2` `) ` `-` `2` `);` ` ` `# Value of N is odd` ` ` `else` `:` ` ` `return` `(` `pow` `(` `2` `, (N ` `+` `1` `) ` `/` `2` `) ` `+` ` ` `pow` `(` `2` `, (N ` `+` `1` `) ` `/` `2` `) ` `-` `2` `);` `# Driver code` `N ` `=` `2` `;` `print` `(` `int` `(findAllSequence(N)));` `# This code is contributed by mits` |

## C#

`// C# program to find` `// number of sequences` `using` `System;` `public` `class` `GFG{` ` ` ` ` `// function to calculate` ` ` `// total sequences possible` ` ` `static` `int` `findAllSequence(` `int` `N)` ` ` `{` ` ` `// Value of N is even` ` ` `if` `(N % 2 == 0)` ` ` `{` ` ` `return` `(` `int` `)(Math.Pow(2, N / 2 + 1) +` ` ` `Math.Pow(2, N / 2) - 2);` ` ` `}` ` ` `// Value of N is odd` ` ` `else` ` ` `{` ` ` `return` `(` `int` `)(Math.Pow(2, (N + 1) / 2) +` ` ` `Math.Pow(2, (N + 1) / 2) - 2);` ` ` `}` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `Main ()` ` ` `{` ` ` `int` `N = 2;` ` ` `Console.WriteLine( findAllSequence(N));` ` ` `}` `}` `// This code is contributed by 29AjayKumar` |

## PHP

`<?php` `// PHP program to find number of sequences` `// function to calculate total` `// sequences possible` `function` `findAllSequence(` `$N` `)` `{` ` ` `// Value of N is even` ` ` `if` `(` `$N` `% 2 == 0)` ` ` `{` ` ` `return` `pow(2, ` `$N` `/ 2 + 1) +` ` ` `pow(2, ` `$N` `/ 2) - 2;` ` ` `}` ` ` ` ` `// Value of N is odd` ` ` `else` ` ` `{` ` ` `return` `pow(2, (` `$N` `+ 1) / 2) +` ` ` `pow(2, (` `$N` `+ 1) / 2) - 2;` ` ` `}` `}` `// Driver code` `$N` `= 2;` `echo` `findAllSequence(` `$N` `);` `// This code is contributed by mits` `?>` |

## Javascript

`<script>` ` ` `// Javascript program to find number of sequences` ` ` ` ` `// function to calculate` ` ` `// total sequences possible` ` ` `function` `findAllSequence(N)` ` ` `{` ` ` ` ` `// Value of N is even` ` ` `if` `(N % 2 == 0)` ` ` `{` ` ` `return` `(Math.pow(2, N / 2 + 1) +` ` ` `Math.pow(2, N / 2) - 2);` ` ` `}` ` ` ` ` `// Value of N is odd` ` ` `else` ` ` `{` ` ` `return` `(Math.pow(2, (N + 1) / 2) +` ` ` `Math.pow(2, (N + 1) / 2) - 2);` ` ` `}` ` ` `}` ` ` ` ` `let N = 2;` ` ` `document.write(findAllSequence(N));` ` ` `</script>` |

**Output:**

4

**Time Complexity**: O(LogN)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**