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 :

Let fo(N) and fe(N) be the functions 
that are defined for the odd and even values of N respectively.

 fo(N) =  fe(N-1) + 2(N-1)/2
 fe(N) =  fo(N-1) + 2N/2

From above equation compute
 fo(N) = fo(N-2) + 2(N-1)/2 + 2(N-1)/2
 fe(N) = fe(N-2) + 2N/2 + 2(N-2)/2

Base Case: 
 fo(1) = 2
 fe(0) = 1

By using the above equation, compute the following results :
 fo(N) - fo(N-2) =  2(N-1)/2 + 2(N-1)/2
 fo(N) - fo(N-2) =  2(N+1)/2 

By taking the sum of above equation for all odd values of N, 
below thing is computed.  
 fo(N) - fo(N-2) + fo(N-1) - fo(N-3) + ...... + fo(3) - fo(1) = 
 22 + 23 + 24 + ..... + 2(N+1)/2

Hence on summation,
fo(N) - fo(1) = SUM[ n = 0 to (N+1)/2 ] 2n - 21 - 20

By using sum of geometric progression
fo(N) = 2( N + 1 ) / 2 + 2( N + 1 ) / 2 - 2

Similarly, find fe(N) :
 fe(N) = fe(N-2) + 2N/2 + 2(N-2)/2
 fe(N) - fe(0) = SUM[ n = 0 to N/2 ] 2n - 20 - SUM[ n = 0 to (N-1)/2 ] 2n

By using sum of geometric progression
fe(N) = 2 (N / 2) + 1 + 2 N / 2 - 2

The final formula will be as follows:

f(N) = 2(N+1)/2 + 2(N+1)/2 – 2, if N is odd

f(N) = 2(N/2) + 1 + 2N/2 – 2, if N is even

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Output:

4

Time Complexity: O(LogN)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : vt_m, 29AjayKumar, Mithun Kumar