"""Difference bases: minimize |B|^2 / v (EinsteinArena difference-bases, MIT). v = largest integer such that 1..v are all differences of elements of B (0 is added to B if missing). Ported from the upstream verifier. Changes: elements must be JSON integers in [0, inst["max_value"]] (upstream cast with int(), so floats/strings were silently truncated) and distinct (upstream deduplicated); at most inst["max_size"] elements after adding 0.""" import numpy as np from run import Invalid, int_list def check(inst, ans): s = ans.get("set") if not isinstance(s, list) or not 1 <= len(s) <= inst["max_size"]: raise Invalid(f"set must be a list of 1..{inst['max_size']} integers") int_list(s, None, 0, inst["max_value"], "set") if len(set(s)) != len(s): raise Invalid("elements must be distinct") B = sorted(set(s) | {0}) if len(B) > inst["max_size"]: raise Invalid(f"at most {inst['max_size']} elements including 0") a = np.array(B, dtype=np.int64) d = (a[None, :] - a[:, None])[np.triu_indices(len(a), 1)] present = np.zeros(int(a[-1]) + 2, dtype=bool) present[d] = True present[0] = True v = int(np.argmin(present)) - 1 # first missing positive difference minus one if v < 1: raise Invalid("1 is not a difference of the set") return len(B) ** 2 / v, {"size": len(B), "v": v}