# Number of sequences which has HEAD at alternate positions to the right of the first HEAD

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 than all the further alternating positions to the right are 5, 7, 9…

**Examples:**

Input:N = 2

Output:4

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

Input:N = 3

Output:6

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

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

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

**Approach:**

If the sequence is to start with a tail, then all possible sequences of length N-1 is 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 rest of the N/2 places can be either head or tail. Thus the recursive formula for 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}– 2

f(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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity**: O(LogN)

## Recommended Posts:

- Count Number of animals in a zoo from given number of head and legs
- Program to find the head start in a race
- Find a number containing N - 1 set bits at even positions from the right
- Number of different positions where a person can stand
- Number of N length sequences whose product is M
- Count total number of even sum sequences
- Print all non-increasing sequences of sum equal to a given number x
- Number of positions such that adding K to the element is greater than sum of all other elements
- Minimum number of consecutive sequences that can be formed in an array
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Print all sequences of given length
- Count of sub-sequences which satisfy the given condition
- Find a way to fill matrix with 1's and 0's in blank positions
- Minimum cost to cover the given positions in a N*M grid

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.