Date: 2026-06-06
Time: 18:14
This file solves LeetCode 2367 — Number of Arithmetic Triplets. Given a strictly increasing array nums and an integer diff, it counts how many triplets (i, j, k) satisfy i < j < k where nums[j] - nums[i] == diff and nums[k] - nums[j] == diff.
countarithmetictriplets(nums, diff) -> intThe sole exported function. It uses a single-pass, set-based lookup approach rather than the naive O(n³) triple-nested loop.
Contract:
nums is a strictly increasing list of integers; diff is a positive integer.diff.Set-based membership testing. Instead of checking all (i, j, k) combinations, the function builds a seen set as it iterates. For each element x, it checks whether both x - diff and x - 2*diff already exist in seen. If so, (x - 2*diff, x - diff, x) forms a valid triplet.
This is a standard technique for reducing lookup complexity — converting an O(n) scan per element into O(1) hash lookups.
Single-pass accumulation. The loop processes each element exactly once, adding it to seen *after* checking for triplet membership. This ordering is correct because nums is strictly increasing — by the time we reach x, any valid x - diff and x - 2*diff must appear earlier and already be in the set.
Imports: None — pure standard library (uses built-in set).
Imported by: number-of-arithmetic-triplets/test_solution.py directly. The massive "Imported By" list in the prompt is misleading — those are unrelated test files that likely share a common test harness, not actual consumers of this function.
1. Initialize empty seen set and count = 0.
2. For each x in nums (left to right):
x - diff is in seen (middle element exists).x - 2 * diff is in seen (first element exists).count — x is the largest element of a valid triplet.x to seen.3. Return count.
nums being strictly increasing. Duplicate values would still work (the set just wouldn't grow), but the problem guarantees uniqueness.seen.add(x) happens after the triplet check. This prevents an element from being part of its own triplet, though with strictly increasing values and positive diff, self-reference is impossible anyway.seen set grows to hold all elements.This is optimal compared to the brute-force O(n³) and the two-pointer O(n²) alternatives.
None. The function trusts its inputs match the LeetCode contract (valid list, positive diff). No bounds checking, type validation, or exception handling — appropriate for a competitive programming solution.
number-of-arithmetic-triplets/test_solution.py — See which edge cases the tests cover (empty arrays, single-element, large diff)number-of-arithmetic-triplets/plan.md — The original approach analysis before implementationnumber-of-arithmetic-triplets/review.md — Post-implementation review notes and alternative approachescount-good-triplets/solution.py:countgoodtriplets — Another triplet-counting problem; compare how it handles a more complex constraint (three separate bounds instead of a single diff)set-lookup-optimization — This pattern (build a set while scanning, check membership for complements) recurs across two-sum, pair-diff, and triplet problems in this repoarithmetic-triplets-linear-time — countarithmetictriplets runs in O(n) time and O(n) space via set-based lookups, not brute-force enumerationarithmetic-triplets-backward-lookup — The algorithm treats each element as the *largest* of a potential triplet and looks backward for x - diff and x - 2*diff, which is correct because iteration is left-to-right on a sorted arrayarithmetic-triplets-no-imports — The solution uses only Python builtins (set, int) with zero external or standard library importsarithmetic-triplets-insert-after-check — Each element is added to seen after the triplet membership check, ensuring the invariant that only previously-visited elements are candidates