This problem rephrases an interview question. Since this original problem seems too hard to me I am trying to solve a simpler one: how to process an integer array to find an average of any sub-array in constant time. Obviously, we can process all sub-arrays in O(n^2). Are there better solutions?
This problem rephrases an interview question . Since this original problem seems too hard
Share
For the 1d case: compute the cumulative sums of the array, i. e. given array
a, definebbyTo compute any average of a subarray, compute the difference of the cumultaive sum corrseponding to the end index and the one corresponding to the start index, and divide by the number of entries in the subarray. For example for the range from
i+1toj, doThe same thing works in two dimensions by computing cumulative sums along the two axes.