Skip to the code
cf.
+10 XP

#393 Median Page Weight

easy

A web performance report lists the transfer size of every page on a site in kilobytes. The report shows the median size: the middle value after sorting, or the average of the two middle values when there is an even number of pages.

Write medianWeight(weights) that returns the median. weights is not sorted and has at least one value. Answers within 1e-5 are accepted.

Example 1

Input: weights = [300,120,450]
Output: 300

Sorted: 120, 300, 450. The middle is 300.

Example 2

Input: weights = [80,20,60,40]
Output: 50

Sorted: 20, 40, 60, 80. The middle two average to (40 + 60) / 2 = 50.

Constraints

  • 1 <= weights.length <= 10^5
  • 0 <= weights[i] <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).