#2465

Number of Distinct Averages

pupil · 390 · lc easy +24 · verified · 58.9% accepted · 427 likes · top 56%

Description

Given a 0-indexed integer array nums of even length, repeatedly extract the minimum and maximum elements, compute their average (using integer division), and collect all distinct averages.

Return the number of distinct averages produced.

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.

Code

1
2
3