A group of trees is planted along a straight line.
KOI is planning to plant more trees so that the distance between two adjacent trees is equal for all trees.
For simplicity, each tree can only be planted on an integer coordinate.
To illustrate this problem I drew this small graphic:
The large trees are given and we have to find the small ones. You can see pretty fast that the maximum difference between trees is the minimum difference between already planted trees. But we’re not only looking for one arbitrary solution (which would be zero difference), we’re looking for the greatest difference or more generally the Greatest common divisor.
def gcd(a, b):
if b == 0:
return gcd(b, a % b)
myGcd = gcd(lst, lst)
for c in lst[1:]:
myGcd = gcd(myGcd, c)
N = int(raw_input())
diff = 
lastTree = int(raw_input())
for i in xrange(1, N):
newTree = int(raw_input())
diff.append(newTree - lastTree)
lastTree = newTree
myGcd = gcdList(diff)
trees = map(lambda x: (x / myGcd) - 1, diff)