# Average between n numbers [closed]

Is it possible to calculate the average between n numbers in a static random access array of some size in less than $O(n)$ time? I know that the trivial approach is $O(n)$, but can I do it in less time using a data structure or a certain preparation? or both? I am asking for hints, not a solution.

Note: The array is of some length. What and I mean by between is when given 2 indexes a and b, I have to calculate the average of the sub array $[a+1,b-1]$. And I have to keep it updated while the data structure is managed, that's why I said it was a dynamic set, in other words a random access array. I do not know of any restrictions, just asking if it's possible in less than $O(n)$.

• Is the array of length n? what precisely do you mean by "between"? – d'alar'cop Jun 2 '14 at 13:26
• What is $n$? The number of elements? Do you need to compute the average once or keep it updated while the data structure is managed? If the latter, is the running time restricted for a sngle run or for all of them combined? – FrankW Jun 2 '14 at 13:27
• The array is of some length. What and I mean by between is when given 2 indexes a and b, I have to calculate the average of the sub array [a+1,b-1]. And I have to keep it updated while the data structure is managed, that's why I said it was a dynamic set, in other words a random access array. I do not know of any restrictions, just asking if it's possible in less than $O(n)$. – HaloKiller Jun 2 '14 at 13:31
• So you keep the average updating as you add/remove elements?... the subindices stay the same? – d'alar'cop Jun 2 '14 at 13:36
• @Trinarics You are only specifying your full question in the comments! Please explain your question in the question statement itself. The way it is currently stated, the average can be maintained in $O(1)$ time. – Yuval Filmus Jun 2 '14 at 15:20

I'll assume that you are computing the average of the elements of an array of fixed size and that you can change the value at some position at any moment. You want a data structure that supports the following two operations:

1. Update element at location $i$,
2. Query sum of elements in range [$i$, $j$].

A binary indexed tree (aka Fenwick tree) performs both in $\cal O(\lg n)$.

I am assuming that the query must be done in less than O(n) time and preprocessing can take any amount of time.

Pre-processing: Store the values in a Binary Tree like data structure.Every node of tree has sum of its subtree and number of children(To calculate average).So the base case is that you have n leaves having n values and we go leaves to root to construct this tree.You can do this pre-processing in O(nlogn) time.

Query: Now whenever you update a value you need to go to leaf along the path from top node to this leaf and the sum only changes in the nodes along this path so this takes only O(logn) time.

Correct me if I am wrong.

• How do you compute the average for a subarray that does not align with a subtree? – FrankW Jun 2 '14 at 13:43
• My approach cannot compute it,I assumed question asks average of n elements where n is given to you and n is the size of array – iLoveCamelCase Jun 2 '14 at 14:38
• Also user mentioned update is O(1) so my method would be invalid anyway. – iLoveCamelCase Jun 2 '14 at 14:39
• My method has same complexity as above answer – iLoveCamelCase Jun 2 '14 at 14:45