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.
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 :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Find the player who will win the Coin game
- Python program for word guessing game
- Number Guessing Game in Python using Binary Search
- Find the winner of the game based on greater number of divisors
- C Program to Add two Integers
- Find N distinct integers with sum N
- Find N distinct integers with zero sum
- Find N integers with given difference between product and sum
- Longest Subarray of non-negative Integers
- Find if it is possible to choose subarray that it contains exactly K even integers
- Most frequent factor in a range of integers
- Ways to write N as sum of two or more positive integers | Set-2
- Maximum possible GCD for a pair of integers with product N
- Find the first repeating element in an array of integers
- Check if a number can be written as sum of three consecutive 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 firstname.lastname@example.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.