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…
Input: N = 2
All possible combinations will be TT, TH, HT, HH.
Input: N = 3
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.
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:
Time Complexity: O(LogN)
- 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 firstname.lastname@example.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.