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.
Input: C = ‘H’, N = 5
Output: Head = 2, Tail = 3
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
After all the possible flips the head and tail count is 4 and 3.
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:
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.
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Total length of string from given Array of strings composed using given characters
- Number of ways to sum up a total of N from limited denominations
- Find the final number obtained after performing the given operation
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Longest palindromic string possible after removal of a substring
- Maximum inversions in a sequence of 1 to N after performing given operations at most K times
- Final direction after visiting every cell of Matrix starting from (0, 0)
- First number to leave an odd remainder after repetitive division by 2
- Mth element after K Right Rotations of an Array
- Largest number made up of X and Y with count of X divisible by Y and of Y by X
- Count of sub-strings with equal consecutive 0's and 1's
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- Count of occurrences of a "1(0+)1" pattern in a string
- Count of number of given string in 2D character array
- Count Occurrences of Anagrams
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.
Improved By : VirusBuddah_