AI Glossary / Graphs and symbolic AI / Intermediate
Range minimum query
Definition
A range minimum query, or RMQ, asks for the smallest value in a specified contiguous interval of an array. Some formulations return the position of that value rather than the value itself. The algorithmic challenge is usually to prepare the array so that many different interval queries can be answered efficiently.
What it means
Suppose an array stores one measurement for each successive time slot. An RMQ can ask for the lowest measurement between two chosen slots without sorting the observations or changing their order. The interval is defined by array positions, not by selecting all values within a numerical band.
The simplest solution scans the requested interval. This is often sufficient for a few short queries. When many queries target the same data, preprocessing can store useful summaries and avoid repeated scans. The best approach depends on whether the array stays fixed, how many queries arrive, and whether values must be updated between queries.
How it works
For a static array, a sparse table stores minima for intervals whose lengths are powers of two. Larger stored intervals are built by combining two smaller ones. To answer a nonempty query of length L, choose the largest power of two not exceeding L and use one block starting at the query's left end and another ending at its right end.
These two blocks cover the query and may overlap. Taking the minimum of their minima is valid because considering the same value twice does not change a minimum. With suitable constant-time indexing, the usual sparse table uses O(n log n) preprocessing and storage and O(1) query time. Dynamic structures use different trade-offs when updates matter.
Three practical examples
- A monitoring dashboard can repeatedly retrieve the minimum observed reading over user-selected intervals in a fixed historical series.
- A text-processing algorithm can use RMQ as a supporting operation over arrays derived from strings, rather than scanning each interval repeatedly.
- A planning tool can inspect the lowest available capacity across a sequence of time slots, provided its capacity data and update rules match the chosen structure.
Think of it this way
Think of keeping the lowest reading for many reusable spans in a notebook. A new interval can sometimes be answered by combining a small number of prepared spans. The analogy is limited because the span choices and overlap rules are mathematical; combining summaries incorrectly can include values outside the requested interval or double-count quantities for other operations.
Worked example
Consider an original array A = [8, 3, 6, 2, 7, 2, 9, 5], using zero-based indexing and inclusive endpoints. The query from index 1 through index 6 examines [3, 6, 2, 7, 2, 9]. Its minimum value is 2, occurring at indices 3 and 5. If the interface returns an index, specify a tie rule; choosing the leftmost returns 3.
The query length is 6, so the largest power of two no greater than it is 4. The first stored block covers indices 1 through 4, with values [3, 6, 2, 7] and minimum 2. The second covers indices 3 through 6, with values [2, 7, 2, 9] and minimum 2. Their union is exactly the requested interval. Taking min(2, 2) returns 2.
The blocks overlap at indices 3 and 4. Overlap is harmless for a minimum, but it would be wrong to answer a sum query by simply adding both block sums. Those sums are 18 and 20, totaling 38, whereas the requested interval sums to 29. The duplicated values 2 and 7 explain the excess of 9.
Now change A[3] from 2 to 10. The queried minimum value remains 2 at index 5, but a stored answer pointing to index 3 is stale. A static sparse table does not automatically track this update; maintaining correctness requires rebuilding relevant information or choosing an update-capable approach.
Illustrative case study
Fictional case: Ash Archive displays historical sensor minima for many selected date windows. The team initially scans every interval, then measures that repeated long queries dominate response time. It evaluates preprocessing because the archived daily snapshot is fixed.
For the live dashboard, where readings can be corrected, it uses a separate update-aware design and tests stale-summary cases. This fictional example illustrates workload-based selection. It does not claim a particular speedup or imply that one RMQ structure is best for both immutable archives and frequently changing data.
Common misconceptions and mistakes
Do not confuse the minimum value with the index where it occurs. Ties can be irrelevant for one interface and essential for another. Be explicit about zero-based versus one-based indexing and whether the right endpoint is included.
Prefix minima cannot generally answer arbitrary interval minima by subtraction as prefix sums can. Information about an earlier minimum can hide later values. The overlapping-block trick also depends on the operation: minimum tolerates duplication, while sum does not. Empty ranges, missing values, and invalid indices need defined behavior rather than accidental implementation results.
Business use
RMQ is a supporting data-structure problem rather than an AI model in itself. It can make analysis and retrieval components efficient when the same ordered data receives many interval questions.
Before investing in a sophisticated structure, measure the number and lengths of queries, available memory, and update frequency. Include preprocessing and maintenance costs in the comparison. Validate answers against direct scans on representative small datasets, including tied minima and boundary intervals. A fast answer is useful only when its interval and data version match the user's request.
People also ask
Does RMQ require the array to be sorted?
No. Sorting would usually destroy the original interval meaning.
Why can sparse-table blocks overlap?
Repeating a value does not change a minimum, so combining overlapping minima remains valid.
Is constant-time querying free?
No. It depends on prior preparation, storage, and the assumptions of the selected structure.
Check your understanding
What should an index-returning query report for indices 1 through 6 after A[3] changes to 10?
Answer: Index 5, where the remaining minimum value 2 occurs. Returning the old index 3 would be incorrect.
Related terms
- Array
- Preprocessing
- sparse table
- segment tree
- interval query
- Cartesian tree
- lowest common ancestor
- algorithmic complexity
Learn next
Query indices 4 through 7 in the example. Their values are [7, 2, 9, 5], so the minimum is 2 at index 5. Then query a single-element interval and explain why its only value is necessarily its minimum.
More in Graphs and symbolic AI
- Random-restart hill climbing - Random-restart hill climbing is a local-search strategy that runs hill climbing from several randomly selected starting states.
- Random walk - A random walk is a sequence of states produced by repeatedly making a random step from the current state.
- Randomness - Randomness refers to variation described through uncertain outcomes and probability rules rather than a fixed predictable outcome in the relevant model.
- Rao-Blackwellization - Rao - Blackwellization is a way to improve an estimator by replacing part of its random variation with an appropriate conditional expectation.
- Rapidly exploring dense trees - Rapidly exploring dense trees, or RDTs, are incremental exploration structures that grow a tree through a configuration space using a dense sequence of...
- Rapidly exploring random tree - A rapidly exploring random tree, or RRT, is a sampling-based planning structure that grows a branching tree through a state or configuration space.
Sources
Sources: Stanford CS166, original Range Minimum Queries, Part I lecture and condensed slides, including static preprocessing and sparse tables: https://web.stanford.edu/class/archive/cs/cs166/cs166.1266/lectures/00/ .
Previous: Range coding Next: Range query (database)