Skip to main content

Compute Median

MediumStatisticsSortingArray

Description

Given an array of numbers nums, return the median. For an even count, return the average of the two middle values. Round to 4 decimal places.

Examples

Input:nums = [3,1,2]
Output:2
Explanation:

Sorting the values and taking the middle gives a median of 2.

Input:nums = [1,2,3,4]
Output:2.5
Explanation:

Sorting the values and taking the middle gives a median of 2.5.

Input:nums = [5,5,5,5]
Output:5
Explanation:

Sorting the values and taking the middle gives a median of 5.

Constraints

  • 1 ≤ nums.length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.