Rolling Median Absolute Deviation
Problem statement
For each consecutive window of exactly k values, return its unscaled median absolute deviation (MAD). First find the window median m, then take the median of the k values abs(x - m).
For an odd number of values, the median is the middle sorted value. For an even number, it is the arithmetic mean of the two middle sorted values. Apply this same rule to the original values and to the deviations. Do not multiply by a normalization factor.
Return the MAD values in increasing window-start order. If fewer than k values are available, including an empty input, return an empty array. Do not mutate the input.
Function
rollingMad(values: int[], k: int) → double[]Examples
Example 1
values = [1,2,10,4]k = 3return = [1.0,2.0]The windows are [1,2,10] and [2,10,4]. Their medians are 2 and 4. The sorted deviations are [0,1,8] and [0,2,6], so their MAD values are 1 and 2.
Example 2
values = [1,2,8,9]k = 4return = [3.5]The median is (2+8)/2=5. The sorted absolute deviations are [3,3,4,4], whose median is 3.5.
Example 3
values = [5,5]k = 3return = []There are only two values, so no complete window of three exists.
Constraints
0 <= values.length <= 500-1000000 <= values[i] <= 10000001 <= k <= 500