- Related Questions & Answers
- C# program to split the Even and Odd integers into different arrays
- Odd Even Jump in C++
- Even numbers at even index and odd numbers at odd index in C++
- Count subarrays with same even and odd elements in C++
- Number of integers with odd number of set bits in C++
- Divide Two Integers in C++
- Matching odd even indices with values in JavaScript
- Count number of ordered pairs with Even and Odd Product in C++
- Count number of ordered pairs with Even and Odd Sums in C++
- C/C++ Program for Odd-Even Sort (Brick Sort)?
- Odd even index difference - JavaScript
- Odd Even Linked List in Python
- Separate odd and even in JavaScript
- Largest Even and Odd N-digit numbers in C++
- C Program to Add two Integers

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

**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*

* *

#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); }

The return value of even odd game is 3

Advertisements