|
| 1 | +# Chef is playing a game on a sequence of N positive integers, say A1, A2, ... AN. The game is played as |
| 2 | +# follows. |
| 3 | +# If all the numbers are equal, the game ends. |
| 4 | +# Otherwise |
| 5 | +# Select two numbers which are unequal |
| 6 | +# Subtract the smaller number from the larger number |
| 7 | +# Replace the larger number with the result from above (see the explanation section for clarity) |
| 8 | +# Chef has already figured out that the game always terminates. He also knows, for a given sequence of |
| 9 | +# integers, the game will always terminate on the same value, no matter how the game is played. Chef wants |
| 10 | +# you to simulate the game for him and tell him on which value will the game terminate for a given sequence |
| 11 | +# of integers. |
| 12 | +# |
| 13 | +# Input |
| 14 | +# The first line of the input contains an integer T, the number of test cases. Then follow the |
| 15 | +# description of T test cases. The first line of each test case contains a single integer N, the length of |
| 16 | +# the sequence. The second line contains N positive integers, each separated by a single space. |
| 17 | +# |
| 18 | +# Output |
| 19 | +# For each test case, output a single integer - the value of all the numbers when they are equal (and the game terminates), on a line by itself. |
| 20 | +# |
| 21 | +# Constraints |
| 22 | +# 1 ≤ T ≤ 100 |
| 23 | +# 1 ≤ N ≤ 1000 |
| 24 | +# 1 ≤ Ai ≤ 109 |
| 25 | +# |
| 26 | +# Sample |
| 27 | +# Input |
| 28 | +# 3 |
| 29 | +# 2 |
| 30 | +# 10 12 |
| 31 | +# 2 |
| 32 | +# 5 9 |
| 33 | +# 3 |
| 34 | +# 6 10 15 |
| 35 | +# |
| 36 | +# Output |
| 37 | +# 2 |
| 38 | +# 1 |
| 39 | +# 1 |
| 40 | +# |
| 41 | +# Explanation |
| 42 | +# Test Case 1: Since there are only two numbers, the operations are forced. |
| 43 | +# |
| 44 | +# { 10, 12 } => Replace 12 with ( 12 - 10 = 2 ) => { 10, 2 } |
| 45 | +# { 10, 2 } => Replace 10 with ( 10 - 2 = 8 ) => { 8, 2 } |
| 46 | +# { 8, 2 } => Replace 8 with ( 8 - 2 = 6 ) => { 6, 2 } |
| 47 | +# { 6, 2 } => Replace 6 with ( 6 - 2 = 4 ) => { 4, 2 } |
| 48 | +# { 4, 2 } => Replace 4 with ( 4 - 2 = 2 ) => { 2, 2 } |
| 49 | +# The value of all the numbers when the game ends is 2. |
| 50 | +# |
| 51 | +# Test Case 2: Since there are only two numbers, the operations are forced. |
| 52 | +# |
| 53 | +# { 5, 9 } => Replace 9 with ( 9 - 5 = 4 ) => { 5, 4 } |
| 54 | +# { 5, 4 } => Replace 5 with ( 5 - 4 = 1 ) => { 1, 4 } |
| 55 | +# { 1, 4 } => Replace 4 with ( 4 - 1 = 3 ) => { 1, 3 } |
| 56 | +# { 1, 3 } => Replace 3 with ( 3 - 1 = 2 ) => { 1, 2 } |
| 57 | +# { 1, 2 } => Replace 2 with ( 2 - 1 = 1 ) => { 1, 1 } |
| 58 | +# The value of all the numbers when the game ends is 1. |
| 59 | +# |
| 60 | +# Test Case 3: One way to play the game is |
| 61 | +# |
| 62 | +# { 6, 10, 15 } => Replace 15 with ( 15 - 6 = 9 ) => { 6, 10, 9 } |
| 63 | +# { 6, 10, 9 } => Replace 10 with ( 10 - 6 = 4 ) => { 6, 4, 9 } |
| 64 | +# { 6, 4, 9 } => Replace 9 with ( 9 - 6 = 3 ) => { 6, 4, 3 } |
| 65 | +# { 6, 4, 3 } => Replace 6 with ( 6 - 4 = 2 ) => { 2, 4, 3 } |
| 66 | +# { 2, 4, 3 } => Replace 3 with ( 3 - 2 = 1 ) => { 2, 4, 1 } |
| 67 | +# { 2, 4, 1 } => Replace 4 with ( 4 - 2 = 2 ) => { 2, 2, 1 } |
| 68 | +# { 2, 2, 1 } => Replace first 2 with ( 2 - 1 = 1 ) => { 1, 2, 1 } |
| 69 | +# { 1, 2, 1 } => Replace 2 with ( 2 - 1 = 1 ) => { 1, 1, 1 } |
| 70 | + |
| 71 | +for _ in range(int(input())): |
| 72 | + n = int(input()) |
| 73 | + numbers = [int(i) for i in input().split()] |
| 74 | + res = 0 |
| 75 | + while True: |
| 76 | + maxi = max(numbers) |
| 77 | + mini = min(numbers) |
| 78 | + res = maxi - mini |
| 79 | + numbers[numbers.index(maxi)] = res |
| 80 | + if(n == numbers.count(res)): |
| 81 | + print(res) |
| 82 | + break |
0 commit comments