Paint the Array (Codeforces 1618C)
Tags: Math, math

You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. You have to choose a positive integer $$$d$$$ and paint all elements into two colors. All elements which are divisible by $$$d$$$ will be painted red, and all other elements will be painted blue.The coloring is called beautiful if there are no pairs of adjacent elements with the same color in the array. Your task is to find any value of $$$d$$$ which yields a beautiful coloring, or report that it is impossible.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases.The first line of each testcase contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the number of elements of the array.The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{18}$$$).

Output: For each testcase print a single integer. If there is no such value of $$$d$$$ that yields a beautiful coloring, print $$$0$$$. Otherwise, print any suitable value of $$$d$$$ ($$$1 \le d \le 10^{18}$$$).


Sample Inputs:
5
5
1 2 3 4 5
3
10 5 15
3
100 10 200
10
9 8 2 6 6 2 8 6 5 4
2
1 3

Sample Outputs:
2
0
100
0
3

Solution (Python):
import sys
input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
import math
for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    if n == 2:
        print(max(l) if l[0] != l[1] else 0)
    elif n == 1:
        print(l[0])
    elif n > 2:
        f = 0
        g = l[0]
        for i in range(2, n, 2):
            g = math.gcd(l[i], g)
        for i in range(1, n, 2):
            if l[i] % g == 0:
                f = 1
                break
        if f == 0:
            print(g)
            continue
        f = 0
        g = l[1]
        for i in range(3, n, 2):
            g = math.gcd(l[i], g)
        for i in range(0, n, 2):
            if l[i] % g == 0:
                f = 1
                break
        if f == 0:
            print(g)
            continue
        print(0)

Solution (Python):
import math

def res():
    n = int(input())
    a = list(map(int, input().split()))
    y = 0
    for i in range(0, n, 2):
        y = math.gcd(y, a[i])
    x = True
    for i in range(1, n, 2):
        if a[i] % y == 0:
            x = False
    if x:
        print(y)
        return
    y = 0
    for i in range(1, n, 2):
        y = math.gcd(y, a[i])
    x = True
    for i in range(0, n, 2):
        if a[i] % y == 0:
            x = False
    if x:
        print(y)
        return
    print(0)
o = int(input())
for v in range(o):
    res()

Solution (Python):
import sys
import copy
import heapq
import bisect
import functools
from math import *
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import groupby
import itertools
from itertools import accumulate
from decimal import Decimal, ROUND_HALF_UP
readline = lambda : sys.stdin.readline().rstrip()

def solve():
    n = int(readline())
    a = list(map(int, readline().split()))
    for c in [0, 1]:
        g = 0
        for i in range(c, n, 2):
            g = gcd(g, a[i])
        for i in range((c + 1) % 2, n, 2):
            if a[i] % g == 0:
                break
        else:
            print(g)
            return
    print(0)

def main():
    t = 1
    t = int(readline())
    for i in range(t):
        solve()
if __name__ == '__main__':
    main()