Petya is preparing for IQ test and he has noticed that there many problems like: you are given a sequence, find the next number. Now Petya can solve only problems with arithmetic or geometric progressions.

Arithmetic progression is a sequence a_1, a_1 + d, a_1 + 2d, ..., a_1 + (n - 1)d, where a_1 and d are any numbers.

Geometric progression is a sequence b_1, b_2 = b_1q, ..., b_{n} = b_{n} - 1q, where b_1 ≠ 0, q ≠ 0, q ≠ 1. 

Help Petya and write a program to determine if the given sequence is arithmetic or geometric. Also it should found the next number. If the sequence is neither arithmetic nor geometric, print 42 (he thinks it is impossible to find better answer). You should also print 42 if the next element of progression is not integer. So answer is always integer.


-----Input-----

The first line contains exactly four integer numbers between 1 and 1000, inclusively.


-----Output-----

Print the required number. If the given sequence is arithmetic progression, print the next progression element. Similarly, if the given sequence is geometric progression, print the next progression element.

Print 42 if the given sequence is not an arithmetic or geometric progression.


-----Examples-----
Input
836 624 412 200

Output
-12

Input
1 334 667 1000

Output
1333



-----Note-----

This problem contains very weak pretests!

Difficulty: MEDIUM_HARD
Tags: Implementation

Starter code:

Solutions:
a = list(map(int, input().split()))
isGeom = True
isAlg = True
d = a[1] - a[0]
q = a[1] / a[0]
for i in range(2, 4):
	if a[i] - a[i - 1] != d:
		isAlg = False
	if a[i] / a[i - 1] != q:
		isGeom = False
if isAlg:
	print(a[3] + d)
elif isGeom:
	if a[3] * q != int(a[3] * q):
		print(42)
	else:
		print(int(a[3] * q))
else:
	print(42)


(a, b, c, d) = list(map(int, input().split()))
a1 = b - a
a2 = c - b
a3 = d - c
g1 = b / a
g2 = c / b
g3 = d / c
if a1 == a2 == a3:
	res = d + a1
	print(res)
elif g1 == g2 == g3 and d * g1 == int(d * g1):
	res = g1 * d
	print(int(res))
else:
	print(42)


(a, b, c, d) = map(int, input().split())
if b - a == c - b == d - c:
	print(d + (d - c))
elif b * b == a * c and c * c == b * d and (d * d % c == 0):
	print(d * d // c)
else:
	print(42)


a = list(map(int, input().split()))
n = len(a)
o = 1
d = a[1] - a[0]
ok = 1
for i in range(n - 1):
	if a[i + 1] - a[i] != d:
		ok = 0
if ok:
	print(a[n - 1] + d)
	o = 0
d = a[1] / a[0]
ok = 1
for i in range(n - 1):
	if a[i + 1] / a[i] != d:
		ok = 0
if ok and o:
	o = 0
	if int(a[n - 1] * d) == a[n - 1] * d:
		print(int(a[n - 1] * d))
	else:
		o = 1
if o:
	print(42)


(a, b, c, d) = map(int, input().split())
r = 42
if b - a == c - b == d - c:
	r = d + b - a
if b / a == c / b == d / c == int(d * c / b) / d:
	r = int(d * c / b)
print(r)


def getints(str):
	return list(map(int, str.split(' ')))
a = getints(input())
if a[1] - a[0] == a[2] - a[1] == a[3] - a[2]:
	print(2 * a[3] - a[2])
elif 0 not in a and a[1] / a[0] == a[2] / a[1] == a[3] / a[2]:
	if a[3] * a[3] % a[2] == 0:
		print(a[3] * a[3] // a[2])
	else:
		print(42)
else:
	print(42)


l = list(map(int, input().split()))
x = l[1] - l[0]
y = l[2] - l[1]
z = l[3] - l[2]
xx = l[1] / l[0]
yy = l[2] / l[1]
zz = l[3] / l[2]
if x == y == z:
	print(l[3] + x)
elif xx == yy == zz:
	s = l[3] * xx
	if int(s) == s:
		print(int(s))
	else:
		print(42)
else:
	print(42)


def test_arithmetic(in_list):
	arithmetic = True
	current_nr = in_list[0]
	dif = in_list[1] - in_list[0]
	for nr in in_list[1:]:
		if nr - current_nr != dif:
			arithmetic = False
		else:
			current_nr = nr
	if arithmetic:
		next = nr + dif
		return next
	else:
		return 42

def test_geometric(in_list):
	geometric = True
	for nr in in_list:
		if nr == 0:
			return False
	else:
		quotient = in_list[1] / in_list[0]
		current_nr = in_list[0]
		for nr in in_list[1:]:
			if nr / current_nr != quotient:
				geometric = False
			else:
				current_nr = nr
		if geometric:
			next = nr * quotient
			if int(next) == 0 or int(next) / nr != quotient:
				return 42
			else:
				return next
		else:
			return 42
in_numbers = list(map(int, input().split()))
answer_ari = test_arithmetic(in_numbers)
answer_geo = test_geometric(in_numbers)
if answer_ari != 42:
	print(int(answer_ari))
else:
	print(int(answer_geo))


import sys
fin = sys.stdin
a = list(map(int, fin.readline().split()))
d = a[1] - a[0]
if a[2] - a[1] == a[3] - a[2] == d:
	print(a[3] + d)
else:
	d = a[1] / a[0]
	if a[2] / a[1] == a[3] / a[2] == d and d * a[3] == int(d * a[3]):
		print(int(d * a[3]))
	else:
		print(42)


s = input().split()
for i in range(4):
	s[i] = int(s[i])
if s[1] - s[0] == s[2] - s[1] == s[3] - s[2]:
	d = s[1] - s[0]
	if (s[3] + d) % 1 == 0:
		print(s[3] + d)
	else:
		print(42)
elif s[1] / s[0] == s[2] / s[1] == s[3] / s[2]:
	d = s[1] / s[0]
	if int(s[3] * d) != s[3] * d:
		print(42)
	else:
		print(int(s[3] * d))
else:
	print(42)


import sys
n = input().split()
a = int(n[0])
b = int(n[1])
c = int(n[2])
d = int(n[3])
if b - a == c - b == d - c:
	print(2 * d - c)
	sys.exit()
if b * b == a * c and c * c == b * d:
	if d * d % c == 0:
		print(d * d // c)
	else:
		print(42)
	sys.exit()
print(42)


def is_arithmatic(seq):
	return all([seq[i] - seq[i - 1] == seq[i - 1] - seq[i - 2] for i in range(2, len(seq))])

def is_geometric(seq):
	return all([seq[i] / seq[i - 1] == seq[i - 1] / seq[i - 2] for i in range(2, len(seq))])

def main():
	numbers = input()
	numbers = numbers.split()
	for i in range(0, len(numbers)):
		numbers[i] = int(numbers[i])
	if is_arithmatic(numbers):
		print(numbers[len(numbers) - 1] + (numbers[len(numbers) - 1] - numbers[len(numbers) - 2]))
	elif is_geometric(numbers):
		next_number = numbers[len(numbers) - 1] * (numbers[len(numbers) - 1] / numbers[len(numbers) - 2])
		if next_number == int(next_number):
			print(int(next_number))
		else:
			print(42)
	else:
		print(42)
main()


numbers = list(map(int, input().split()))
arithmetic_prog = numbers[1] - numbers[0]
geometric_prog = numbers[1] / numbers[0]
if numbers[3] - numbers[2] == arithmetic_prog and numbers[2] - numbers[1] == arithmetic_prog:
	print(numbers[-1] + arithmetic_prog)
elif numbers[3] / numbers[2] == geometric_prog and numbers[2] / numbers[1] == geometric_prog:
	if numbers[3] * geometric_prog - int(numbers[3] * geometric_prog) == 0:
		print(int(numbers[-1] * geometric_prog))
	else:
		print(42)
else:
	print(42)


class CodeforcesTask328ASolution:

	def __init__(self):
		self.result = ''
		self.progression = []

	def read_input(self):
		self.progression = [int(x) for x in input().split(' ')]

	def process_task(self):
		arit = [self.progression[x] - self.progression[x - 1] for x in range(1, len(self.progression))]
		if arit[0] == arit[1] == arit[2]:
			self.result = str(self.progression[-1] + arit[0])
		else:
			geo = [self.progression[x] / self.progression[x - 1] for x in range(1, len(self.progression))]
			if geo[0] == geo[1] == geo[2]:
				if (geo[0] * self.progression[-1]).is_integer():
					self.result = str(int(geo[0] * self.progression[-1]))
				else:
					self.result = '42'
			else:
				self.result = '42'

	def get_result(self):
		return self.result
Solution = CodeforcesTask328ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


a = [int(i) for i in input().split()]
flag = False
d1 = a[1] - a[0]
d2 = a[2] - a[1]
d3 = a[3] - a[2]
q1 = a[1] / a[0]
q2 = a[2] / a[1]
q3 = a[3] / a[2]
if d1 == d2 == d3:
	print(a[3] + d1)
	flag = True
elif q1 == q2 == q3:
	otv = a[3] * q1 - int(a[3] * q1)
	tmp = 0 + otv
	if tmp <= 0:
		print(int(a[3] * q1))
		flag = True
if flag == False:
	print('42')


from fractions import Fraction
v = [int(x) for x in input().split()]
d = [v[i + 1] - v[i] for i in range(3)]
if d[0] == d[1] and d[1] == d[2]:
	print(v[3] + d[0])
	exit(0)
r = [Fraction(v[i + 1], v[i]) for i in range(3)]
f = v[3] * r[0]
if r[0] == r[1] and r[1] == r[2] and (f.denominator == 1):
	print(f)
	exit(0)
print(42)


nums = [int(x) for x in input().split()]
history1 = []
history2 = []
for idx in range(4):
	if idx != 3:
		(a, b) = (nums[idx], nums[idx + 1])
		history1.append(b - a)
		history2.append(b / a)
if len(set(history1)) == 1:
	print(nums[-1] + history1[0])
elif len(set(history2)) == 1 and nums[-1] * history2[0] == int(nums[-1] * history2[0]):
	print(int(nums[-1] * history2[0]))
else:
	print(42)


arr = [int(i) for i in input().split()]
q = arr[1] / arr[0]
d = arr[1] - arr[0]
if arr[2] / arr[1] == q and arr[3] / arr[2] == q:
	if int(arr[3] * q) == arr[3] * q:
		print(int(arr[3] * q))
	else:
		print(42)
elif arr[2] - arr[1] == d and arr[3] - arr[2] == d:
	print(arr[3] + d)
else:
	print(42)


def isZ(a):
	return a == int(a)

def geom(a, b, c, d):
	if 0 in (a, b, c, d) and (not a == b == c == d == 0):
		return False
	if b / a == c / b == d / c:
		nxt = d * (d / c)
		if not isZ(nxt):
			return False
		print(int(nxt))
		return True
	return False

def ar(a, b, c, d):
	if b - a == c - b == d - c:
		print(int(2 * d - c))
		return True
	return False
(a, b, c, d) = map(float, input().split())
if not geom(a, b, c, d) and (not ar(a, b, c, d)):
	print(42)


import sys

def solve():
	a = list(map(int, input().split()))
	diffs = list()
	for i in range(3):
		diffs.append(a[i + 1] - a[i])
	if diffs[0] == diffs[1] == diffs[2]:
		return a[3] + diffs[0]
	if a[1] / a[0] == a[2] / a[1] == a[3] / a[2]:
		val = a[3] * diffs[2] / diffs[1]
		if abs(val - int(val)) < 1e-06:
			return int(val)
	return 42
if sys.hexversion == 50594544:
	sys.stdin = open('test.txt')
print(solve())


import sys
c = [int(s) for s in input().split()]
d = c[1] - c[0]
c1 = [c[0]]
for i in range(len(c)):
	c1.append(c1[-1] + d)
d1 = c[1] / c[0]
c2 = [c[0]]
for i in range(len(c)):
	c2.append(c2[-1] * d1)
if c + [c[-1] + d] == c1:
	print(c1[-1])
	sys.exit()
if c + [c[-1] * d1] == c2 and c2[-1] == int(c2[-1]):
	print(int(c2[-1]))
	sys.exit()
print(42)


gi = lambda : list(map(int, input().split()))
a = gi()
d = {a[1] - a[0], a[2] - a[1], a[3] - a[2]}
if len(d) == 1:
	print(a[3] + a[3] - a[2])
	exit()
d = {a[1] / a[0], a[2] / a[1], a[3] / a[2]}
if len(d) == 1 and a[3] * (a[1] / a[0]) == int(a[3] * (a[1] / a[0])):
	print(int(a[3] * (a[1] / a[0])))
	exit()
print(42)


def isInt(n):
	return int(n) == float(n)
a = list(map(int, input().split()))
arifm = []
geom = []
da = a[1] - a[0]
dg = a[1] / a[0]
for i in range(1, len(a)):
	arifm.append(a[i] - a[i - 1])
	geom.append(a[i] / a[i - 1])
arifm = set(arifm)
geom = set(geom)
if len(arifm) > 1 and len(geom) > 1:
	print(42)
elif len(arifm) == 1:
	print(a[len(a) - 1] + da)
elif len(geom) == 1 and isInt(a[len(a) - 1] * dg) == 1:
	print(int(a[len(a) - 1] * dg))
else:
	print(42)


def main():
	(a, b, c, d) = [int(i) for i in input().split()]
	(p1, p2, p3) = (b - a, c - b, d - c)
	(q1, q2, q3) = (b / a, c / b, d / c)
	if p1 == p2 == p3:
		print(d + p3)
	elif q1 == q2 == q3 and q1 != 1 and (int(d * q3) == d * q3):
		print(int(d * q3))
	else:
		print(42)
main()