The online part is what makes this tricky.
First, clarify the problem constraints and edge cases, then transform the average condition into a prefix sum condition: (prefix[j] - prefix[i]) / (j - i) = S implies prefix[j] - S*j = prefix[i] - S*i. Use a hash map to track the earliest occurrence of each transformed prefix value, and for each new element, check if the current transformed prefix has been seen before to compute the longest valid subarray ending at the current index.
Pro tip: Mention that the transformed prefix values can be non-integers if S is not an integer, so use a hash map with double keys or scale values to avoid floating-point precision issues. Also, note that the longest subarray might not end at the current index, so maintain the global maximum length.
Ask about constraints: Is S an integer? Can the subarray be empty? What should be returned if no such subarray exists? Confirm that the stream is infinite and we need to answer after each element.
Derive that the average condition is equivalent to (prefix[j] - prefix[i]) = S * (j - i), which rearranges to prefix[j] - S*j = prefix[i] - S*i. Define a transformed prefix value T(k) = prefix[k] - S*k.
Use a hash map to store the first occurrence of each transformed prefix value. Initialize with T(0) = 0 at index 0. For each new element, update the prefix sum and compute T(current index).
After computing T(i), check if it exists in the hash map. If yes, the subarray from the stored index+1 to i has average S, so update the maximum length. If not, store T(i) with index i.
After each element, output the current maximum length. If no valid subarray has been found, return 0 or as specified.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.