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: interview

Starter code:

Solutions:
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)

a, b, c, d = list(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)


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)


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 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)

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)

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)
 
    

#import doctest
#doctest.testmod()
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)
    


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]
    # print(diffs)
    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-6:
            return int(val)
    return 42

if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
print(solve())

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]
    # print(diffs)
    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-6:
            return int(val)
    return 42

if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
print(solve())

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 = [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')

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)

a,b,c,d = list(map(int, input().split()))
if d - c == c - b and c - b == b - a:
    print(d + d - c)
elif b*d == c*c and a*c == b*b and (d*d)%c == 0:
    print(d*d//c)
else:
    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)

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 and int(d * b / a) == d * b / a:
    r = int(d * c / b)
    
print(r)

a,b,c,d=list(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)


a,b,c,d=list(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)


a,b,c,d=list(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)


a,b,c,d=list(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)


a,b,c,d=list(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)


a,b,c,d=list(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)


a,b,c,d=list(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)


a,b,c,d=list(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)


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)