Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed integer array nums of even length.
As long as nums is not empty, you must repetitively:
nums and remove it.nums and remove it.The average of two numbers a and b is (a + b) / 2.
2 and 3 is (2 + 3) / 2 = 2.5.Return the number of distinct averages calculated using the above process.
Note that when there is a tie for a minimum or maximum number, any can be removed.
Example 1:
Input: nums = [4,1,4,0,3,5] Output: 2 Explanation: 1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3]. 2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3]. 3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5. Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
Example 2:
Input: nums = [1,100] Output: 1 Explanation: There is only one average to be calculated after removing 1 and 100, so we return 1.
Constraints:
2 <= nums.length <= 100nums.length is even.0 <= nums[i] <= 100Problem summary: You are given a 0-indexed integer array nums of even length. As long as nums is not empty, you must repetitively: Find the minimum number in nums and remove it. Find the maximum number in nums and remove it. Calculate the average of the two removed numbers. The average of two numbers a and b is (a + b) / 2. For example, the average of 2 and 3 is (2 + 3) / 2 = 2.5. Return the number of distinct averages calculated using the above process. Note that when there is a tie for a minimum or maximum number, any can be removed.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers
[4,1,4,0,3,5]
[1,100]
two-sum)finding-pairs-with-a-certain-sum)minimum-average-of-smallest-and-largest-elements)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2465: Number of Distinct Averages
class Solution {
public int distinctAverages(int[] nums) {
Arrays.sort(nums);
Set<Integer> s = new HashSet<>();
int n = nums.length;
for (int i = 0; i < n >> 1; ++i) {
s.add(nums[i] + nums[n - i - 1]);
}
return s.size();
}
}
// Accepted solution for LeetCode #2465: Number of Distinct Averages
func distinctAverages(nums []int) (ans int) {
sort.Ints(nums)
n := len(nums)
s := map[int]struct{}{}
for i := 0; i < n>>1; i++ {
s[nums[i]+nums[n-i-1]] = struct{}{}
}
return len(s)
}
# Accepted solution for LeetCode #2465: Number of Distinct Averages
class Solution:
def distinctAverages(self, nums: List[int]) -> int:
nums.sort()
return len(set(nums[i] + nums[-i - 1] for i in range(len(nums) >> 1)))
// Accepted solution for LeetCode #2465: Number of Distinct Averages
impl Solution {
pub fn distinct_averages(nums: Vec<i32>) -> i32 {
let mut nums = nums;
nums.sort();
let n = nums.len();
let mut cnt = vec![0; 201];
let mut ans = 0;
for i in 0..n >> 1 {
let x = (nums[i] + nums[n - i - 1]) as usize;
cnt[x] += 1;
if cnt[x] == 1 {
ans += 1;
}
}
ans
}
}
// Accepted solution for LeetCode #2465: Number of Distinct Averages
function distinctAverages(nums: number[]): number {
nums.sort((a, b) => a - b);
const s: Set<number> = new Set();
const n = nums.length;
for (let i = 0; i < n >> 1; ++i) {
s.add(nums[i] + nums[n - i - 1]);
}
return s.size;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.