Sliding-Window Means with IEEE Special Values
Problem statement
You are given an integer array valueBits and an integer k. Each integer is the raw 32-bit IEEE 754 representation of one single-precision floating-point value.
For every complete contiguous window of length k, compute its arithmetic mean and return the mean's raw 32-bit representation. Return the results from left to right.
Handle special values as follows:
- If a window contains any NaN, its result is the canonical quiet NaN bit pattern
0x7fc00000. - If a window contains both positive and negative infinity, its result is the same canonical NaN.
- Otherwise, a window containing positive infinity has result
+Infinity, and a window containing negative infinity has result-Infinity. - For a finite window, add its values using double precision, divide by
k, convert the result to single precision, and return its raw bits. Canonicalize a zero result to positive zero.
Use a sliding-window update rather than recomputing every window from scratch.
Function
slidingWindowMeanBits(valueBits: int[], k: int) → int[]Examples
Example 1
valueBits = [1065353216,1073741824,1077936128,1082130432]k = 2return = [1069547520,1075838976,1080033280]The input bits represent [1.0, 2.0, 3.0, 4.0]. The window means are [1.5, 2.5, 3.5], represented by the returned bits.
Example 2
valueBits = [1065353216,2139095040,1077936128,-8388608]k = 3return = [2139095040,2143289344]The first window is [1.0, +Infinity, 3.0], so its mean is positive infinity. The next window contains both infinities, so its result is canonical NaN.
Example 3
valueBits = [2143289344,1084227584,-8388608]k = 1return = [2143289344,1084227584,-8388608]With window size one, canonical NaN, 5.0, and negative infinity are returned unchanged.
Constraints
1 <= valueBits.length <= 200000.1 <= k <= valueBits.length.- Every integer in
valueBitsis interpreted as one raw IEEE 754 single-precision bit pattern. - Any NaN payload is treated as NaN; every NaN output uses
0x7fc00000.