Let's say we have a large array of integers A. We want to answer to many queries like:
- Find the minimum between index 0 and 100
- Find the minimum between index 4 and 90
- ...
Example: A = {6, 1, 7, 5, 3}
- min between indexes 0 and 1 is 1
- min between indexes 2 and 3 is 5
- min between indexes 0 and 4 is 1
The obvious way of traversing the elements for each query and finding the minimum is not enough regarding performance. I somehow need to store the required information in one pass, and then answer to the queries in constant time. So the algorithm should not be quadratic. Something better than O(N * M) is needed. (N: array size, M: number of queries)
I tried but could not find how to do that. It must be something about finding and storing some sums and using them somehow. Any ideas? Thanks for reading.