# Even-odd turn game with two integers

Given three positive integers X, Y and P. Here P denotes the number of turns. Whenever the turn is odd X is multiplied by 2 and in every even turn Y is multiplied by 2. The task is to find the value of max(X, Y) ÷ min(X, Y) after the complete P turns.

**Examples :**

Input : X = 1, Y = 2, P = 1 Output : 1 As turn is odd, X is multiplied by 2 and becomes 2. Now, X is 2 and Y is also 2. Therefore, 2 ÷ 2 is 1. Input : X = 3, Y = 7, p = 2 Output : 2 Here we have 2 turns. In the 1st turn which is odd X is multiplied by 2. And the values are 6 and 7. In the next turn which is even Y is multiplied by 2. Now the final values are 6 and 14. Therefore, 14 ÷ 6 is 2.

Lets play the above game for 8 turns :

| i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |------|---|----|----|----|----|----|----|-----|-----| | X(i) | X | 2X | 2X | 4X | 4X | 8X | 8X | 16X | 16X | | Y(i) | Y | Y | 2Y | 2Y | 4Y | 4Y | 8Y | 8Y | 16Y |

Here we can easily spot a pattern :

if i is even, then X(i) = z * X and Y(i) = z * Y. if i is odd, then X(i) = 2*z * X and Y(i) = z * Y.

Here z is actually the power of 2. So, we can simply say –

If P is even output will be max(X, Y) ÷ min(X, Y) else output will be max(2*X, Y) ÷ min(2*X, Y).

Below is the implementation :

## C++

`// CPP program to find max(X, Y) / min(X, Y) ` `// after P turns ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `findValue(` `int` `X, ` `int` `Y, ` `int` `P) ` `{ ` ` ` `if` `(P % 2 == 0) ` ` ` `return` `(max(X, Y) / min(X, Y)); ` ` ` ` ` `else` ` ` `return` `(max(2 * X, Y) / min(2 * X, Y)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// 1st test case ` ` ` `int` `X = 1, Y = 2, P = 1; ` ` ` `cout << findValue(X, Y, P) << endl; ` ` ` ` ` `// 2nd test case ` ` ` `X = 3, Y = 7, P = 2; ` ` ` `cout << findValue(X, Y, P) << endl; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find max(X, Y) / min(X, Y) ` `// after P turns ` `import` `java.util.*; ` ` ` `class` `Even_odd{ ` ` ` `public` `static` `int` `findValue(` `int` `X, ` `int` `Y, ` ` ` `int` `P) ` ` ` `{ ` ` ` `if` `(P % ` `2` `== ` `0` `) ` ` ` `return` `(Math.max(X, Y) / ` ` ` `Math.min(X, Y)); ` ` ` ` ` `else` ` ` `return` `(Math.max(` `2` `* X, Y) / ` ` ` `Math.min(` `2` `* X, Y)); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// 1st test case ` ` ` `int` `X = ` `1` `, Y = ` `2` `, P = ` `1` `; ` ` ` `System.out.println(findValue(X, Y, P)); ` ` ` ` ` `// 2nd test case ` ` ` `X = ` `3` `; ` ` ` `Y = ` `7` `; ` ` ` `P = ` `2` `; ` ` ` `System.out.print(findValue(X, Y, P)); ` ` ` `} ` `} ` ` ` `//This code is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find max(X, Y) / min(X, Y) ` `# after P turns ` ` ` `def` `findValue( X , Y , P ): ` ` ` `if` `P ` `%` `2` `=` `=` `0` `: ` ` ` `return` `int` `(` `max` `(X, Y) ` `/` `min` `(X, Y)) ` ` ` ` ` `else` `: ` ` ` `return` `int` `(` `max` `(` `2` `*` `X, Y) ` `/` `min` `(` `2` `*` `X, Y)) ` ` ` `# Driver code ` `# 1st test case ` `X ` `=` `1` `Y ` `=` `2` `P ` `=` `1` `print` `(findValue(X, Y, P)) ` ` ` `# 2nd test case ` `X ` `=` `3` `Y ` `=` `7` `P ` `=` `2` `print` `((findValue(X, Y, P))) ` ` ` `# This code is contribted by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find max(X, Y) / min(X, Y) ` `// after P turns ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `int` `findValue(` `int` `X, ` `int` `Y, ` ` ` `int` `P) ` ` ` `{ ` ` ` `if` `(P % 2 == 0) ` ` ` `return` `(Math.Max(X, Y) / ` ` ` `Math.Min(X, Y)); ` ` ` ` ` `else` ` ` `return` `(Math.Max(2 * X, Y) / ` ` ` `Math.Min(2 * X, Y)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `// 1st test case ` ` ` `int` `X = 1, Y = 2, P = 1; ` ` ` `Console.WriteLine(findValue(X, Y, P)); ` ` ` ` ` `// 2nd test case ` ` ` `X = 3; ` ` ` `Y = 7; ` ` ` `P = 2; ` ` ` `Console.WriteLine(findValue(X, Y, P)); ` ` ` `} ` `} ` ` ` `//This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// max(X, Y) / min(X, Y) ` `// after P turns ` ` ` `function` `findValue(` `$X` `, ` `$Y` `, ` `$P` `) ` `{ ` ` ` `if` `(` `$P` `% 2 == 0) ` ` ` `return` `(int)(max(` `$X` `, ` `$Y` `) / ` ` ` `min(` `$X` `, ` `$Y` `)); ` ` ` ` ` `else` ` ` `return` `(int)(max(2 * ` `$X` `, ` `$Y` `) / ` ` ` `min(2 * ` `$X` `, ` `$Y` `)); ` `} ` ` ` `// Driver code ` ` ` `// 1st test case ` `$X` `= 1; ` `$Y` `= 2; ` `$P` `= 1; ` `echo` `findValue(` `$X` `, ` `$Y` `, ` `$P` `), ` `"\n"` `; ` ` ` `// 2nd test case ` `$X` `= 3; ` `$Y` `= 7; ` `$P` `= 2; ` `echo` `findValue(` `$X` `, ` `$Y` `, ` `$P` `), ` `"\n"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1 2

**Time Complexity:**O(1)

## Recommended Posts:

- What happens when we turn on computer?
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Game of stones
- Water Game
- Predict the winner in Coin Game
- Python program for word guessing game
- C Program to Add two Integers
- Most frequent factor in a range of integers
- Ways to write N as sum of two or more positive integers | Set-2
- Find N integers with given difference between product and sum
- Longest Subarray of non-negative Integers
- Find the first repeating element in an array of integers
- Check if a number can be written as sum of three consecutive integers
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Recursive program to print formula for GCD of n integers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.