# Python # Java code for the above approach
# Recursive function to return gcd of a and b
def __gcd(a, b):
# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a
# Base case
if (a == b):
return a
# a is greater
if (a > b):
return __gcd(a - b, b)
return __gcd(a, b - a)
# Function to find the gcd of the array
def gcdofarray(arr, start, N):
result = arr[start]
for i in range(start + 2, N, 2):
result = __gcd(arr[i], result)
return result
# Function to check if the whole set
# is not divisible by gcd
def check(arr, start, gcd, N):
for i in range(start, N, 2):
# If any element is divisible
# by gcd return 0
if (arr[i] % gcd == 0):
return False
return True
# Function to find the value x
def find_divisor(arr, N):
# Find gcds of values at odd indices
# and at even indices separately
gcd1 = gcdofarray(arr, 1, N)
gcd2 = gcdofarray(arr, 0, N)
# If both the gcds are not same
if (gcd1 != gcd2):
if (check(arr, 0, gcd1, N)):
x = gcd1
print(x)
return
if (check(arr, 1, gcd2, N)):
x = gcd2
print(x)
return
# If both the gcds are same print-1
print(-1)
# Driver Code
# Initialize the array
arr = [6, 5, 9, 10, 12, 15]
N = len(arr)
# Call the function
find_divisor(arr, N)
# This code is contributed by Shubham Singh