Number of Distinct Averages
In this lab, you will implement a function to find the number of distinct averages calculated from a given even-length integer array. You are given a 0-indexed integer array nums
of even length. The goal is to perform a set of operations on the array until it is empty and count the number of distinct averages computed using the following process:
- 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
and3
is(2 + 3) / 2 = 2.5
.
Note that when there is a tie for a minimum or maximum number, any can be removed.
Examples
Example 1:
Input: nums = [4,1,4,0,3,5]
Output: 2
Explanation:
- Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums =
[4,1,4,3]
. - Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums =
[4,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 <= 100
nums.length
is even.0 <= nums[i] <= 100
Challenges
Complete the following challenges:
- Write the
distinctAverages
function to compute the number of distinct averages of the given input arraynums
. - Export the
distinctAverages
function from theindex.js
file.
Evaluation Script
The evaluation script will test your implementation by using dynamic imports, which means you have to ensure that the distinctAverages
function is exported in your index.js
file. The order of the tests should match the order of the challenges.
The final length of the array testlog
in the evaluation script must be equal to the number of challenges. In this case, there are 2 challenges. Make sure you properly write the evaluation script to avoid any errors.
Good luck!