File: number-of-distinct-averages/solution.py

Date: 2026-06-06

Time: 18:17

number-of-distinct-averages/solution.py

Purpose

Solves LeetCode 2465: Number of Distinct Averages. The problem asks: given an even-length array, repeatedly remove the current min and max, compute their average, and return how many distinct averages you get.

Key Components

distinctAverages(nums: list[int]) -> int — The sole function. Takes an even-length list of integers, returns the count of distinct averages formed by pairing the smallest and largest remaining elements.

Patterns

Two-pointer on sorted array. Rather than simulating removal (which would be O(n²) with a list), the solution sorts once and walks inward with left/right pointers. This is the canonical approach for problems that pair min/max elements.

Sum-instead-of-average trick. The function collects nums[left] + nums[right] into a set instead of computing the actual average (nums[left] + nums[right]) / 2. This works because two averages are distinct if and only if their sums are distinct — dividing by the same constant (2) is a monotonic bijection. This sidesteps floating-point precision issues entirely.

In-place mutation. nums.sort() sorts the input list in-place. The caller's list is modified — acceptable for LeetCode but worth noting.

Dependencies

Imports: None beyond builtins (list, set).

Imported by: The corresponding number-of-distinct-averages/test_solution.py. The "Imported By" list in the prompt is misleading — those are unrelated test files that happen to share a common test harness import pattern, not actual importers of this function.

Flow

1. Sort the array in-place — O(n log n).

2. Initialize an empty set sums and two pointers left=0, right=len(nums)-1.

3. Loop while left < right: add nums[left] + nums[right] to the set, then move both pointers inward.

4. Return len(sums) — the number of distinct sums (equivalently, distinct averages).

Each iteration consumes exactly one min-max pair. Since nums has even length, the loop runs exactly len(nums) // 2 times and exhausts all elements.

Invariants

Error Handling

None. The function trusts the caller to provide a valid even-length list of integers, consistent with LeetCode's guaranteed constraints. Empty input returns 0 (the while-loop body never executes).