Coin Flip Codechef Solution

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 NN coins numbered from 11 to NN, 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 NN rounds. In the kk-th round the player will flip the face of the all coins whose number is less than or equal to kk. That is, the face of coin ii will be reversed, from Head to Tail, or, from Tail to Head, for i≤ki≤k.

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

Input:

  • The first line of input contains an integer TT, denoting the number of test cases. Then TT test cases follow.
  • The first line of each test contains an integer GG, denoting the number of games played by Elephant. Each of the following GG lines denotes a single game, and contains 33 space-separated integers II, NN, QQ, where II denotes the initial state of the coins, NN denotes the number of coins and rounds, and QQ, which is either 11, or 22 as explained below.

Here I=1I=1 means all coins are showing Head in the start of the game, and I=2I=2 means all coins are showing Tail in the start of the game. Q=1Q=1 means Elephant needs to guess the total number of coins showing Head in the end of the game, and Q=2Q=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≤101≤T≤10

1≤G≤20001≤G≤2000

1≤N≤101≤N≤10

1≤I≤21≤I≤2

1≤Q≤21≤Q≤2

Sample Input:

1
2
1 5 1
1 5 2

Sample Output:

2
3

Coin Flip  – CodeChef Solution in JAVA

import java.util.Scanner;

class Codechef
{
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int T=sc.nextInt();
		while(T-->0)
		{
			int G=sc.nextInt();
			while(G-->0)
			{
				int I=sc.nextInt();
			    int N=sc.nextInt();
			    int Q=sc.nextInt();

			    if(I==Q)
			    {
			    	System.out.println(N/2);
			    }
			    else
			    {
			    	System.out.println(N-(N/2));
			    }
			}
		}
	}
}

Coin Flip – CodeChef Solution in CPP

#include <iostream>
using namespace std;

int main() {
	int a;
	cin>>a;
	for(int j=0;j<a;j++)
	{
	    int t;
	    cin>>t;
	    for(int j=0;j<t;j++)
	    {int c, d, e, f=0, g=0;
            cin >> c >> d >> e;
            if (c == 1)
            {
                if (d % 2 == 0)
                {
                    f = d/2;
                    g = d/2;
                    /* code */
                }
                else
                {
                    f = (d + 1) / 2;
                    g = (d - 1) / 2;
                }
                /* code */
            }
            else

            {
                if (d % 2 == 0)
                {
                    f = d/2;
                    g = d/2;
                    /* code */
                }
                else
                {
                    g = (d + 1) / 2;
                    f = (d - 1) / 2;
                }
            }if (e==1)
            {
                cout<<g<<endl;/* code */
            }else
            cout<<f<<endl;
            

	        
	    }
	}
            

	return 0;
}

Coin Flip  -CodeChef Solution in Python

for _ in range(int(input())):
    for _ in range(int(input())):
        I,N,Q=map(int,input().split())
    
        if N%2==0:
            print(N//2)
    
        else:
            x=(N-1)//2
            if I==Q:
                print(x)
            else:
                print(N-x)

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

Leave a Reply

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