Practice/Amazon/Leetcode 571. Find Median Given Frequency of Numbers
CodingMust
You are given two arrays of equal length: values and frequencies. The values array contains distinct integers, and the frequencies array indicates how many times each corresponding value appears in an implicit dataset.
Your task is to calculate the median of this dataset without actually expanding it into a full array. The frequencies can be very large (up to 10^9), making expansion impractical.
The median is defined as:
Return the median as a floating-point number.
Example 1:
Input: values = [1, 2, 3], frequencies = [1, 2, 1] Output: 2.0 Explanation: The implicit dataset is [1, 2, 2, 3] with 4 total elements. The median is the average of the 2nd and 3rd elements: (2 + 2) / 2 = 2.0
Example 2:
Input: values = [10, 20, 30], frequencies = [5, 3, 2] Output: 10.0 Explanation: Total count is 10. The implicit dataset has 5 tens, 3 twenties, and 2 thirties. The median positions are 5th and 6th elements, both are 10.
Example 3:
Input: values = [1, 100], frequencies = [1, 1] Output: 50.5 Explanation: Dataset is [1, 100]. Median is (1 + 100) / 2 = 50.5