Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
gcd with pairwise solution in Clear category for Evenly Spaced Trees by roman.bratishchev
from itertools import pairwise
from math import gcd
def evenly_spaced_trees(trees: list[int]) -> int:
step=gcd( *(diffs:=list(t2-t1 for t1,t2 in pairwise(trees))) )
return sum(diff//step-1 for diff in diffs)
if __name__ == '__main__':
print("Example:")
print(evenly_spaced_trees([0, 2, 6]))
assert evenly_spaced_trees([0, 2, 6]) == 1, 'add 1'
assert evenly_spaced_trees([1, 3, 6]) == 3, 'add 3'
assert evenly_spaced_trees([0, 2, 4]) == 0, 'no add'
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 17, 2024