#C5042. Smallest k for Divisible Sequence

    ID: 48648 Type: Default 1000ms 256MiB

Smallest k for Divisible Sequence

Smallest k for Divisible Sequence

Given a sequence of N integers, you are allowed to add an integer k to every element of the sequence exactly once. Your task is to find the smallest positive integer k such that after the addition, every element becomes divisible by k. In the special case where all elements of the sequence are identical, the answer is defined to be 0, and if the sequence contains a single element, the answer is that element itself.

The key observation is that when you add k to all elements, the relative differences between the elements remain unchanged. Therefore, apart from the single element scenario, k is the greatest common divisor (GCD) of the differences between consecutive elements. In mathematical terms, if the sequence is \(A = [a_1, a_2, \dots, a_N]\) then for \(N > 1\), we define

[ k = \gcd(|a_2 - a_1|, |a_3 - a_2|, \dots, |a_N - a_{N-1}|) ]

This problem involves computing the GCD of differences efficiently and handling edge cases separately.

inputFormat

The input is given via standard input and consists of two lines:

  • The first line contains a single integer N denoting the number of elements in the sequence.
  • The second line contains N space-separated integers representing the sequence \(A\).

outputFormat

Output a single integer on standard output, which is the smallest k satisfying the given conditions.

## sample
3
2 4 6
2

</p>