Coin Flip CodeChef Solution

Hello Programmers In this post, you will know how to solve the Coin Flip CodeChef Solution. Which is a part of CodeChef DSA Learning Series.

Coin Flip CodeChef Solution
Coin Flip CodeChef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Problem

Little Elephant was fond of inventing new games. After a lot of research, Little Elephant came to know that most of the animals in the forest were showing less interest to play the multi-player games.Little Elephant had started to invent single player games, and succeeded in inventing the new single player game named COIN FLIP.

In this game the player will use N coins numbered from 1 to N, and all the coins will be facing in “Same direction” (Either Head or Tail),which will be decided by the player before starting of the game.

The player needs to play N rounds.In the k-th round the player will flip the face of the all coins whose number is less than or equal to k. That is, the face of coin i will be reversed, from Head to Tail, or, from Tail to Head, for i ≤ k.

Elephant needs to guess the total number of coins showing a particular face after playing N rounds. Elephant really becomes quite fond of this game COIN FLIP, so Elephant plays G times. Please help the Elephant to find out the answer.

Input

The first line of input contains an integer T, denoting the number of test cases. Then T test cases follow.

The first line of each test contains an integer G, denoting the number of games played by Elephant. Each of the following G lines denotes a single game, and contains 3 space separeted integers I, N, Q, where I denotes the initial state of the coins, N denotes the number of coins and rounds, and Q, which is either 1, or 2 as explained below.

Here I=1 means all coins are showing Head in the start of the game, and I=2 means all coins are showing Tail in the start of the game. Q=1 means Elephant needs to guess the total number of coins showing Head in the end of the game, and Q=2 means Elephant needs to guess the total number of coins showing Tail in the end of the game.

Output 

For each game, output one integer denoting the total number of coins showing the particular face in the end of the game.

Constraints 

1≤T≤10

1 ≤ G ≤ 20001≤G≤2000

1 ≤ N ≤ 101≤N≤10

1 ≤ I ≤ 21≤I≤2

1 ≤ Q ≤ 21≤Q≤2

Subtasks

There is only a single subtask with 100 points.

Example 

Input:
1
2
1 5 1
1 5 2
Output:
2
3

Explanation

In the 1st game in Example: I=1, so initial arrangement of coins are H H H H H, and now Elephant will play 5 rounds and coin faces will be changed as follows

After the 1st Round: T H H H H

After the 2nd Round: H T H H H

After the 3rd Round: T H T H H

After the 4th Round: H T H T H

After the 5th Round: T H T H T

Finally Q=1, so we need to find the total number of coins showing Head, which is 2.

In the 2nd game in Example: This is similar to the 1st game, except Elephant needs to find the total number of coins showing Tail. So the Answer is 3. (Please see the final state of the coins in the 1st game) 

Coin Flip CodeChef Solution in Java

/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();
        for (int i=0; i<t; i++)
        {
            int G=sc.nextInt();
            for (int j=0; j<G; j++)
            {
                int I = sc.nextInt();
                int N = sc.nextInt();
                int Q = sc.nextInt();
                if (N%2==0) System.out.println(N/2);
                else
                {
                    if(I==Q) System.out.println(N/2);
                    else
                    {
                        System.out.println((N/2)+1);
                    }
                }
            }
        }
    }
}

Coin Flip CodeChef Solution in CPP

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int t ;
    cin >> t;
    while(t--){
        int n;
        cin>>n;
        while(n--){
            int side,coin,q;
            cin>>side>>coin>>q;
            int ans = coin/2;
            if(side==q)cout << ans <<'\n';
            else cout << coin-ans <<'\n';
        }
    }
    return 0;
}

Coin Flip CodeChef Solution in Python

Coming soon

Disclaimer: The above Problem (Coin Flip CodeChef) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Laddu CodeChef Solution

Leave a Reply

Your email address will not be published. Required fields are marked *