How to find the averages of any consecutive numbers in a sequence?

176 Views Asked by At

The practical purpose of this is I want to identify users that use my app on a weekly basis. For each user I can generate a sequence of times of their interactions, and from that I can generate a sequence of the length of time between each interaction.

So given this sequence of lengths of time, how can I find sections of consecutive numbers that have an average of 7 days or less?

As an example, if I had the following sequence: [1, 11, 1, 8, 12]

[1, 11, 1, 8, 12] would be a valid stretch of numbers with an average of 7 or less, but [11, 1, 8, 12] would not be valid. [1, 2, 12] would again be valid.

Ideally, my output for every valid section would be the starting position of the first item and the length of the section. So [1, 11, 1, 8, 12] would be described as [1, 5] and [1, 2, 12] would be described as [3, 3].

There is a brute force, computational approach where I take every item in the sequence as a start point, and calculate the averages of every possible length of following numbers up until the end of the sequence. The number of calculations grows quickly though at a rate of n(n+1)/2 (Imagine for each given sequence of length N finding consecutive sequences of length N, N-1, N-2 etc.)

I ask broadly if there's a more elegant approach that doesn't require a quadratically growing number of individual calculations for means.

1

There are 1 best solutions below

0
On

I think you have to investigate every contiguous sublist, so there's no way around an order $N^2$ algorithm. If you can bound the length of the sublist then you can do this in order $N$.

You can probably speed up the individual computations by noting that if you have an average $A$ of $k$ numbers you can update the value of $A$ and $k$ when you include or exclude a numerical value without having to look to see how $A$ was calculated originally.