Date: 2026-06-06
Time: 15:30
This file solves LeetCode 1502: Can Make Arithmetic Progression From Sequence. It determines whether a given list of integers can be rearranged into an arithmetic progression — a sequence where the difference between consecutive terms is constant.
It lives in the standard per-problem directory structure (<problem-slug>/solution.py) and exports a single function consumed by the co-located test file and, notably, by the test files of hundreds of other problems (see "Dependencies" below).
can_construct(arr: list[int]) -> boolThe sole public function. Contract:
True if some permutation of arr forms an arithmetic progression, False otherwise.arr in-place via sort(). Callers passing a list they still need in original order should pass a copy.Sort-then-scan: The canonical O(n log n) approach for this problem. Sort the array so the only valid arithmetic progression arrangement is the sorted order itself, then verify the constant-difference property in a single linear pass. This avoids the O(n) math-based alternative (compute expected diff from min/max, check membership via a set) which requires handling duplicates and division edge cases.
Early exit: The loop returns False on the first violation, short-circuiting the scan.
Imports: None — pure stdlib Python.
Imported by: The "Imported By" list in the prompt shows 400+ test files referencing this module. That's a repo-level artifact: the test harness likely imports every solution module uniformly (e.g., via a shared conftest or dynamic import pattern), not because those other problems depend on can_construct at runtime.
1. arr.sort() — in-place ascending sort.
2. Compute diff = arr[1] - arr[0] — the expected common difference.
3. Iterate i from 2 to len(arr) - 1. For each element, check whether arr[i] - arr[i-1] == diff.
4. If any pair violates the constant difference, return False immediately.
5. If the loop completes, return True.
arr[0] and arr[1] unconditionally. Arrays of length 0 or 1 would raise IndexError. The LeetCode constraint guarantees length >= 2.None. The function trusts its caller to provide a list of at least 2 integers, matching the LeetCode problem constraints. No try/except, no input validation. An empty list or single-element list would crash with IndexError at arr[1] - arr[0].
can-make-arithmetic-progression-from-sequence/testsolution.py — See the test cases and how canconstruct is exercised, including edge casesmissing-number-in-arithmetic-progression/solution.py — A related problem that finds the missing element in an AP; likely uses a similar sort-then-scan patterncheck-if-it-is-a-straight-line/solution.py — Another "verify a mathematical property across a sequence" problem with analogous structure (constant slope instead of constant difference)o(n)-arithmetic-progression-check — An alternative approach using min, max, and a set to verify AP membership in O(n) time without sortingrun_tests.py — Explains the cross-cutting test import pattern that causes 400+ test files to list this module as a dependencysort-then-linear-scan — can_construct sorts the input then verifies constant consecutive difference in one pass, making it O(n log n) time and O(1) extra space (beyond the sort)in-place-mutation — can_construct mutates the input list via arr.sort() rather than creating a sorted copyno-input-validation — The function assumes len(arr) >= 2 and will raise IndexError on shorter inputsearly-exit-on-violation — The loop returns False on the first pair whose difference doesn't match arr[1] - arr[0], avoiding unnecessary iteration