Minimum Average of Smallest and Largest Elements

You have an array of floating point numbers averages which is initially empty. You are given an array nums of n integers where n is even.

You repeat the following procedure n / 2 times:

  • Remove the smallest element, minElement, and the largest element, maxElement, from nums.
  • Add (minElement + maxElement) / 2 to averages.

Return the minimum element in averages.

Example 1
Inputnums = [7,8,3,4,15,13,4,1]
Output5.5
The smallest element of averages, 5.5, is returned.
Example 2
Inputnums = [1,9,8,3,10,5]
Output5.5
After repeatedly pairing the smallest and largest elements, averages is [5.5, 6, 6.5], so the minimum is 5.5.

Constraints

  • 2 <= n == nums.length <= 50
  • n is even.
  • 1 <= nums[i] <= 50

Asked at 2 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate