Aman is playing a game on a sequence of N positive integers, say A1, A2, ... AN. The game is played as follows.
*If all the numbers are equal, the game ends.
Otherwise
*Select two numbers which are unequal
**Subtract the smaller number from the larger number
***Replace the larger number with the result from above (see the explanation section for clarity)
Aman has already figured out that the game always terminates. He also knows, for a given sequence of integers, the game will always terminate on the same value, no matter how the game is played. Aman wants you to simulate the game for him and tell him on which value will the game terminate for a given sequence of integers.
Output a single integer - the value of all the numbers when they are equal (and the game terminates), on a line by itself
Sample Case 1:
input(10,12)=2
Explanation:Since there are only two numbers, the operations are forced.
*{ 10, 12 } => Replace 12 with ( 12 - 10 = 2 ) => { 10, 2 }
*{ 10, 2 } => Replace 10 with ( 10 - 2 = 8 ) => { 8, 2 }
*{ 8, 2 } => Replace 8 with ( 8 - 2 = 6 ) => { 6, 2 }
*{ 6, 2 } => Replace 6 with ( 6 - 2 = 4 ) => { 4, 2 }
*{ 4, 2 } => Replace 4 with ( 4 - 2 = 2 ) => { 2, 2 }
Sample Case 2:
input(6,10,15)=1
*If all the numbers are equal, the game ends.
Otherwise
*Select two numbers which are unequal
**Subtract the smaller number from the larger number
***Replace the larger number with the result from above (see the explanation section for clarity)
Aman has already figured out that the game always terminates. He also knows, for a given sequence of integers, the game will always terminate on the same value, no matter how the game is played. Aman wants you to simulate the game for him and tell him on which value will the game terminate for a given sequence of integers.
Output a single integer - the value of all the numbers when they are equal (and the game terminates), on a line by itself
Sample Case 1:
input(10,12)=2
Explanation:Since there are only two numbers, the operations are forced.
*{ 10, 12 } => Replace 12 with ( 12 - 10 = 2 ) => { 10, 2 }
*{ 10, 2 } => Replace 10 with ( 10 - 2 = 8 ) => { 8, 2 }
*{ 8, 2 } => Replace 8 with ( 8 - 2 = 6 ) => { 6, 2 }
*{ 6, 2 } => Replace 6 with ( 6 - 2 = 4 ) => { 4, 2 }
*{ 4, 2 } => Replace 4 with ( 4 - 2 = 2 ) => { 2, 2 }
Sample Case 2:
input(6,10,15)=1

No comments:
Post a Comment