# Even-odd turn game with two integers in C++

C++Server Side ProgrammingProgramming

In this problem, we are given three integer values A , B and T. Our task is to create a program to play Even-odd turn game with two integers.

The two integer value are :

T, that denotes the number of turns in the game.
A denotes the value for player1
B denotes the value for player2

If the value of T is odd, the value of A is multiplied by 2.
If the value of T is even, the value of B is multiplied by 2.
We need to find and return value of max(A, B) / min(A, B) at the end.

## Let’s take an example to understand the problem,

Input: A = 3, B = 4, T = 3

Output: 1

Explanation:

1st Turn : T is odd, A is multiplied by 2, A = 6.
2nd Turn: T is even, B is multiplied by 2, B = 8.
3rd Turn: T is odd, A is multiplied by 2, A = 12.

A = 12                  B = 4

max(A, B) = max(12, 4) = 12
min(A, B) = min(12, 4) = 4
max(A, B) / min(A, B) = 12/ 8 = 1

Solution Approach:

A simple solution to the problem would be calculating the value of A and B after T turns and then return the value of max(A, B) / min(A, B). This is an effective solution by taking T iterations.

But their can be a more effective solution based on the fact that for even value of T, the value of new A is N*A and the value of new B is N*B.

This make the value of max(A, B)  / min(A, B) a  constant which is equal to
max(A, B) / min(A, B).

If the value of T is odd, the value of A will be 2*N*A and the value of B is N*B.

This makes the value of max(A, B) / min(A, B) a constant which is equal to max(2A, B) / min(2A, B).

The result of the problem max(A, B) / min(A, B) =

max(A, B) / min(A, B), if T is even
max(A, B) / min(A, B), if T is odd

## Example

Live Demo

#include <iostream>
using namespace std;

int EvenOddGame(int A, int B, int T) {

if ( T%2 == 0)
return (max(A, B) / min(A, B));
else
return (max(2*A, B) / min(2*A, B));
return -1;
}

int main() {

int A = 3, B = 2, T = 3;
cout<<"The return value of even odd game is "<<EvenOddGame(A, B, T);

}

## Output −

The return value of even odd game is 3

Published on 22-Jan-2021 13:16:29