# Count of total Heads and Tails after N flips in a coin

Given **character C** and an **integer N** representing N number of coins in C position where C can be either **head** or **tail**. We can flip the coins N times, where in the ith round the player will flip the face of all the coins whose number is less than or equal to i. The task is to determine the total number of head and tail after flipping N possible times.

**Examples:**

Input:C = ‘H’, N = 5

Output:Head = 2, Tail = 3

Explanation:

H means initially all the coins are facing in head direction, N means the total number of coins.

So initially for i = 0, we have: H H H H H

After the first round that is i = 1: T H H H H

After the second round that is i = 2: H T H H H

After the third round that is i = 3: T H T H H

After the fourth round that is i = 4: H T H T H

After the fifth round that is i = 5: T H T H T

Hence the total count of the head is 2 and tail is 3.

Input:C = ‘T’, N = 7

Output:Head = 4, Tail = 3

Explanation:

After all the possible flips the head and tail count is 4 and 3.

**Approach: **

To solve the problem mentioned above we have to follow the steps given below:

- In the question above if we observe then there is a pattern that if initially, all the coins are facing towards
**head**direction then total number of heads after N rounds will be floor value of (n / 2) and tails will be the cell value of (n / 2). - Otherwise if all the coins are facing towards
**tail**direction then total number of tails after N rounds will be floor value of (n / 2) and heads will be ceil value of (n / 2).

**Below is the implementation: **

`# Python program to Count total heads ` `# and tails after N flips in a coin ` ` ` `# Function to find count of head and tail ` `import` `math ` `def` `count_ht( s, N ): ` ` ` ` ` `# Check if initially all the ` ` ` `# coins are facing towards head ` ` ` `if` `s ` `=` `=` `"H"` `: ` ` ` `h ` `=` `math.floor( N ` `/` `2` `) ` ` ` `t ` `=` `math.ceil( N ` `/` `2` `) ` ` ` ` ` `# Check if initially all the coins ` ` ` `# are facing towards tail ` ` ` `elif` `s ` `=` `=` `"T"` `: ` ` ` `h ` `=` `math.ceil( N ` `/` `2` `) ` ` ` `t ` `=` `math.floor( N ` `/` `2` `) ` ` ` ` ` `return` `[h, t] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `C ` `=` `"H"` ` ` `N ` `=` `5` ` ` `l ` `=` `count_ht(C, n) ` ` ` `print` `(` `"Head = "` `, l[` `0` `]) ` ` ` `print` `(` `"Tail = "` `, l[` `1` `]) ` |

*chevron_right*

*filter_none*

**Output:**

Head = 2 Tail = 3

**Time complexity:** O(1)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Expected number of coin flips to get two heads in a row?
- Probability of getting K heads in N coin tosses
- Probability of not getting two consecutive heads together in N tosses of coin
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Make a fair coin from a biased coin
- Count total number of even sum sequences
- Count total divisors of A or B in a given range
- Count total number of digits from 1 to n
- Count total set bits in all numbers from 1 to n | Set 2
- Count total unset bits in all the numbers from 1 to N
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count the total number of triangles after Nth operation
- Count of sub-sets of size n with total element sum divisible by 3
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Find the total count of numbers up to N digits in a given base B

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.