Back to problems

Find the Longest Subarray with a Given Mean

Algorithm · Google · Medium

You receive a sequence of integers, which may be positive or negative, one value at a time. Given a target value S, implement longestAverageLengths(S, values), which determines the maximum length of any contiguous subarray seen so far whose average is exactly S. Emit that maximum length after every new value. Example For S = 3, the input stream is: After receiving 4, 2, their sum is 6, so their mean is 3; after receiving 1, the first four values have sum 12, and after 7, the…

Checking your access…