Group By Median

HardData EngineeringArrayMathSorting

Description

Given an array of [key, value] pairs, return [key, median] for each distinct key, where median is the middle value (the average of the two middle values when the count is even) rounded to 4 decimal places. Order by first appearance.

Examples

Input:[[1,10],[1,30],[1,20]]
Output:[[1,20]]
Explanation:

Within each key the values are sorted and the middle one is taken, averaging the two central values when there is an even number.

Input:[[5,1],[5,3]]
Output:[[5,2]]
Explanation:

Within each key the values are sorted and the middle one is taken, averaging the two central values when there is an even number.

Input:[[1,7]]
Output:[[1,7]]
Explanation:

Within each key the values are sorted and the middle one is taken, averaging the two central values when there is an even number.

Constraints

  • 1 ≤ pairs ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.