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, wherein 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 = 5Output:Head = 2, Tail = 3Explanation:

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 = 7Output:Head = 4, Tail = 3Explanation:

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 the total number of heads after N rounds will be the 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 a total number of tails after N rounds will be a floor value of (n / 2) and heads will be ceil value of (n / 2).

Below is the implementation:

## C++

`// C++ program to count total heads` `// and tails after N flips in a coin` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// Function to find count of head and tail` `pair<` `int` `, ` `int` `> count_ht(` `char` `s, ` `int` `N)` `{` ` ` ` ` `// Check if initially all the` ` ` `// coins are facing towards head` ` ` `pair<` `int` `, ` `int` `>p;` ` ` `if` `(s == ` `'H'` `)` ` ` `{` ` ` `p.first = ` `floor` `(N / 2.0);` ` ` `p.second = ` `ceil` `(N / 2.0);` ` ` `}` ` ` ` ` `// Check if initially all the coins` ` ` `// are facing towards tail` ` ` `else` `if` `(s == ` `'T'` `)` ` ` `{` ` ` `p.first = ` `ceil` `(N / 2.0);` ` ` `p.second = ` `floor` `(N / 2.0);` ` ` `}` ` ` ` ` `return` `p;` `}` `// Driver code` `int` `main()` `{` ` ` `char` `C = ` `'H'` `;` ` ` `int` `N = 5;` ` ` ` ` `pair<` `int` `, ` `int` `> p = count_ht(C, N);` ` ` ` ` `cout << ` `"Head = "` `<< (p.first) << ` `"\n"` `;` ` ` `cout << ` `"Tail = "` `<< (p.second) << ` `"\n"` `;` `}` `// This code is contributed by virusbuddah_` |

## Java

`// Java program to count` `// total heads and tails` `// after N flips in a coin` `import` `javafx.util.Pair;` `public` `class` `Main` `{` `// Function to find count of head and tail ` `public` `static` `Pair <Integer,` ` ` `Integer> count_ht(` `char` `s,` ` ` `int` `N)` `{ ` ` ` `// Check if initially all the ` ` ` `// coins are facing towards head ` ` ` `Pair <Integer,` ` ` `Integer> p = ` `new` `Pair <Integer,` ` ` `Integer> (` `0` `, ` `0` `);` ` ` `if` `(s == ` `'H'` `)` ` ` `{` ` ` `p = ` `new` `Pair <Integer,` ` ` `Integer> ((` `int` `)Math.floor(N / ` `2.0` `),` ` ` `(` `int` `)Math.ceil(N / ` `2.0` `));` ` ` `}` ` ` `// Check if initially all the coins ` ` ` `// are facing towards tail ` ` ` `else` `if` `(s == ` `'T'` `)` ` ` `{` ` ` `p = ` `new` `Pair <Integer,` ` ` `Integer> ((` `int` `)Math.ceil(N / ` `2.0` `),` ` ` `(` `int` `)Math.floor(N / ` `2.0` `));` ` ` `}` ` ` `return` `p;` `}` `public` `static` `void` `main(String[] args)` `{` ` ` `char` `C = ` `'H'` `;` ` ` `int` `N = ` `5` `;` ` ` `Pair <Integer,` ` ` `Integer> p = count_ht(C, N);` ` ` `System.out.println(` `"Head = "` `+ p.getKey());` ` ` `System.out.println(` `"Tail = "` `+ p.getValue());` `}` `}` `// This code is contributed by divyeshrabadiya07` |

## Python3

`# Python3 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` `])` |

## C#

`// C# program to count total heads` `// and tails after N flips in a coin` `using` `System;` `class` `GFG{` ` ` `// Function to find count of head and tail ` `public` `static` `Tuple<` `int` `, ` `int` `> count_ht(` `char` `s,` ` ` `int` `N)` `{ ` ` ` ` ` `// Check if initially all the ` ` ` `// coins are facing towards head ` ` ` `Tuple<` `int` `, ` `int` `> p = Tuple.Create(0, 0);` ` ` ` ` `if` `(s == ` `'H'` `)` ` ` `{` ` ` `p = Tuple.Create((` `int` `)Math.Floor(N / 2.0),` ` ` `(` `int` `)Math.Ceiling(N / 2.0));` ` ` `}` ` ` ` ` `// Check if initially all the coins ` ` ` `// are facing towards tail ` ` ` `else` `if` `(s == ` `'T'` `)` ` ` `{` ` ` `p = Tuple.Create((` `int` `)Math.Ceiling(N / 2.0),` ` ` `(` `int` `)Math.Floor(N / 2.0));` ` ` `}` ` ` `return` `p;` `}` `// Driver Code` `static` `void` `Main()` `{` ` ` `char` `C = ` `'H'` `;` ` ` `int` `N = 5;` ` ` `Tuple<` `int` `, ` `int` `> p = count_ht(C, N);` ` ` ` ` `Console.WriteLine(` `"Head = "` `+ p.Item1);` ` ` `Console.WriteLine(` `"Tail = "` `+ p.Item2);` `}` `}` `// This code is contributed by divyesh072019` |

**Output:**

Head = 2 Tail = 3

**Time complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.