#K32747. Minimize the Last Element
Minimize the Last Element
Minimize the Last Element
You are given an array of N integers. You can perform a series of operations on the array. In each operation, you:
- Choose two elements from the array.
- Remove the chosen elements from the array.
- Insert the absolute difference of the two chosen elements into the array.
You can perform these operations as many times as you want until only one element remains. The goal is to minimize the last remaining element. It can be shown that the minimum value obtainable is equal to the greatest common divisor (i.e., \(\gcd(a_1, a_2, \dots, a_n)\)) of all the numbers in the array.
Note: In every test case, the input is given via standard input and the output should be printed to standard output.
inputFormat
The first line of input contains a single integer N (1 \(\leq N \leq 10^4\)), the number of integers in the array.
The second line contains N space-separated positive integers, each between 1 and \(10^4\) (inclusive).
outputFormat
Output a single integer, which is the minimized last remaining element after performing the operations. This is guaranteed to be \(\gcd(a_1, a_2, \dots, a_n)\).
## sample4
4 3 6 8
1