โ† Wiki Index / Computation & Algorithms / Range Minimum Query
๐Ÿ’ป Computation & Algorithms

Range Minimum Query

A range minimum query (RMQ) data structure preprocesses a static array of n comparable elements in linear O(n) time and 2n + o(n) bits of space to return the index of the minimum element inside any query subarray A[i..j] in worst-case O(1) time.

Range minimum query (abbreviated RMQ) is a fundamental indexing problem and static data-structural primitive within computation and algorithms that preprocesses an array A[0 .. n โˆ’ 1] of n totally ordered elements so that, given any pair of indices 0 โ‰ค i โ‰ค j < n, the query RMQA(i, j) returns the index k โˆˆ [i, j] of a minimum element in the contiguous subarray A[i .. j]:

RMQA(i, j) = arg mini โ‰ค k โ‰ค j A[k]

While a naive scan takes O(j โˆ’ i + 1) = O(n) time and a precomputed two-dimensional lookup table over all (i, j) pairs requires ฮ˜(n2) space, classical reductions by Gabow, Bentley, and Tarjan (1984), Berkman and Vishkin (1993), Michael A. Bender and Martin Farach-Colton (2000), and Johannes Fischer and Volker Heun (2011) establish that any static array of n elements can be preprocessed in linear O(n) time to answer every range minimum query in worst-case O(1) time. Furthermore, Fischer and Heun proved that the index can be encoded as a succinct data structure occupying 2n + o(n) bits of memory without even consulting the original array A at query time.

Equivalence Between RMQ and Lowest Common Ancestor (LCA)

From LCA to ยฑ1 RMQ via Euler Tour

The modern theory of range minimum queries rests on an algebraic and combinatorial equivalence between RMQ on arrays and the Lowest Common Ancestor (LCA) problem on rooted trees. Given a rooted tree T with n nodes, the lowest common ancestor LCAT(u, v) of two nodes u and v is the deepest node in T that is an ancestor of both u and v.

An Euler tour of T starts at the root and traverses every tree edge twice (once downward, once upward), visiting a sequence of 2n โˆ’ 1 vertices stored in an Euler tour array E[0 .. 2n โˆ’ 2], along with a corresponding depth array D[0 .. 2n โˆ’ 2] where D[t] is the tree depth of vertex E[t], and a first-occurrence array F[u] recording the first index t where E[t] = u.

For any two vertices u and v with F[u] โ‰ค F[v], the Euler tour must visit LCAT(u, v) at least once between step F[u] and step F[v], and can never rise to a shallower ancestor of LCAT(u, v) before finishing the subtree rooted at LCAT(u, v). Therefore, the lowest common ancestor of u and v is the unique shallowest vertex visited in the subarray E[F[u] .. F[v]]:

LCAT(u, v) = E[ RMQD(F[u], F[v]) ]

Crucially, because every step in an Euler tour moves from a node either to one of its children (depth + 1) or to its parent (depth โˆ’ 1), adjacent entries in the depth array D always differ by +1 or โˆ’1:

|D[t + 1] โˆ’ D[t]| = 1 for all 0 โ‰ค t < 2n โˆ’ 2

This constrained problem is known as ยฑ1 RMQ.

From General RMQ to LCA via the Cartesian Tree

Conversely, any general array A[0 .. n โˆ’ 1] of arbitrary comparable numbers can be reduced to LCA (and hence to ยฑ1 RMQ) in linear O(n) time by constructing the Cartesian tree C(A) introduced by Jean Vuillemin in 1980:

  • The root of C(A) is the index m = RMQA(0, n โˆ’ 1) of the global minimum element of A.
  • The left subtree of m is recursively the Cartesian tree of the prefix subarray A[0 .. m โˆ’ 1], and the right subtree of m is the Cartesian tree of the suffix subarray A[m + 1 .. n โˆ’ 1].

By construction, an in-order traversal of C(A) recovers the array indices 0, 1, โ€ฆ, n โˆ’ 1 in sorted order, while the tree satisfies the min-heap property with respect to values in A. Consequently, for any query range [i, j], the unique index k โˆˆ [i, j] minimizing A[k] is the lowest common ancestor of nodes i and j in the Cartesian tree C(A):

RMQA(i, j) = LCAC(A)(i, j)

Using a monotone stack that scans A from left to right and climbs the rightmost spine of the tree built so far, each element is pushed onto and popped from the rightmost spine at most once, constructing C(A) in O(n) time. Taking the Euler tour of C(A) reduces any general RMQ instance of size n to a ยฑ1 RMQ instance of size 2n โˆ’ 1 in linear time!

Sparse Table and Four Russians Indirection (โŸจO(n), O(1)โŸฉ)

Power-of-Two Sparse Table (โŸจO(n log n), O(1)โŸฉ)

Before applying block compression, the key building block for constant-time range queries is the Sparse Table (Bender and Farach-Colton, 2000). Because the minimum operation is idempotentโ€”meaning min(X, Y) equals min(X, Y) even when two intervals X and Y overlapโ€”any range [i, j] of length L = j โˆ’ i + 1 can be covered by two overlapping intervals of power-of-two length 2k, where k = โŒŠlog2(j โˆ’ i + 1)โŒ‹:

[i, j] = [i, i + 2k โˆ’ 1] โˆช [j โˆ’ 2k + 1, j]

If we precompute a table M[i][k] = RMQA(i, i + 2k โˆ’ 1) for all 0 โ‰ค i < n and 0 โ‰ค k โ‰ค โŒŠlog2 nโŒ‹ by dynamic programming in O(n log n) time and space, then any query RMQA(i, j) is answered in O(1) time by comparing just two array entries: M[i][k] and M[j โˆ’ 2k + 1][k].

Four Russians Block Decomposition for Linear Preprocessing

To reduce the Sparse Table's preprocessing cost from O(n log n) down to O(n) on the ยฑ1 Euler-depth array D[0 .. N โˆ’ 1] (where N = 2n โˆ’ 1), Bender and Farach-Colton apply the Method of Four Russians by partitioning D into N / b contiguous micro-blocks of size b = โŒŠ(1/2) log2 NโŒ‹:

  1. Inter-Block Macro-Array: Form a reduced array B[0 .. N/b โˆ’ 1] of length N / b storing the minimum of each micro-block, and build a standard power-of-two Sparse Table over B. Because B has length O(n / log n), building its Sparse Table takes O((n / log n) ยท log(n / log n)) = O(n) time and space!
  2. Intra-Block Normalized Lookup Table: Within any micro-block of length b, subtracting the block's first element D[t0] yields a normalized relative sequence starting at 0 whose adjacent differences are in {+1, โˆ’1}. Thus, every normalized micro-block is completely determined by a binary vector of b โˆ’ 1 step signs (+1 or โˆ’1). Because there are only 2bโˆ’1 โ‰ค 2(1/2) log2 N = โˆšN distinct normalized block types, precomputing all O(b2) pairwise answers for every possible block type requires only O(โˆšn ยท log2 n) = o(n) time and space!

Any query RMQD(i, j) decomposes into at most two intra-block queries (at the partial left and right boundary blocks, answered in O(1) time via the normalized lookup table) and one contiguous inter-block query over B (answered in O(1) time via the macro Sparse Table).

Range Minimum Query ApproachPreprocessing TimeExtra Space (Bits)Query TimeNeeds Original Array A at Query Time?
Segment Tree over AO(n)O(n log n) bitsO(log n)No (or Yes for index)
Power-of-Two Sparse TableO(n log n)O(n log2 n) bitsO(1)Yes
Bender & Farach-Colton (2000)O(n)O(n log n) bitsO(1)Yes
Fischer & Heun Succinct RMQ (2011)O(n)2n + o(n) bitsO(1)No (index-only 2D min-heap tree)

Fischer and Heun's 2n + o(n)-Bit Succinct RMQ

Because there are Cn = (1 / (n + 1)) ยท (2n choose n) โ‰ˆ 4n / (โˆšฯ€ n3/2) distinct Cartesian trees on n elementsโ€”and two arrays have identical RMQ answers for all pairs (i, j) if and only if they have the same Cartesian treeโ€”the information-theoretic minimum space required to answer RMQ(i, j) without reading A is log2(Cn) = 2n โˆ’ ฮ˜(log n) bits.

In 2011, Johannes Fischer and Volker Heun achieved this information-theoretic bound directly in O(n) construction time and 2n + o(n) bits without converting to an Euler tour (which would double the tree to 2n โˆ’ 1 nodes and cost 4n bits). They converted the Cartesian tree into a 2D-Min-Heap represented by its Depth-First Unary Degree Sequence (DFUDS) balanced-parentheses bitvector of length 2n bits, answering LCA queries directly on the succinct balanced-parentheses bitvector in O(1) time.

Applications in Stringology and Suffix Arrays

Range minimum queries serve as the backbone of modern string indexing. When a suffix array SA[0 .. n โˆ’ 1] is paired with its Longest Common Prefix (LCP) array LCP[1 .. n โˆ’ 1] (where LCP[r] is the length of the longest common prefix between lexicographically adjacent suffixes SA[r โˆ’ 1] and SA[r]), the longest common prefix between any two arbitrary suffixes of a text at positions p and q (with inverse ranks i = ISA[p] < j = ISA[q]) is given by a single constant-time range minimum query over the LCP array:

lcp(T[p..], T[q..]) = LCP[ RMQLCP(i + 1, j) ]

Frequently Asked Questions

Why can't the Sparse Table trick be used for Range Sum Queries in O(1) time?

A Sparse Table covers a query interval [i, j] using two overlapping intervals [i, i + 2k โˆ’ 1] and [j โˆ’ 2k + 1, j]. This works for operations that are idempotent (x โŠ• x = x), such as minimum, maximum, greatest common divisor (gcd), least common multiple (lcm), and bitwise AND/OR, because counting the middle overlap twice does not change the answer. Addition is not idempotent, though static range sum queries are even simpler: a single linear-space prefix-sum array P[k] = โˆ‘t < k A[t] answers any range sum in O(1) time via P[j + 1] โˆ’ P[i]. For non-idempotent non-invertible associative operations (arbitrary semigroups), Disjoint Sparse Tables or Sqrt Trees achieve O(1) query time in O(n log n) space.

How does RMQ handle duplicate minimum values in A?

To guarantee a unique Cartesian tree and a canonical tie-breaking rule between RMQ and LCA, ties in A are broken consistentlyโ€”typically by choosing the leftmost minimum index: A[p] โ‰บ A[q] if and only if A[p] < A[q] or (A[p] = A[q] and p < q).

What if array elements are updated dynamically?

In the dynamic setting where point updates A[k] โ† v are interleaved with RMQ(i, j) queries, Mihai PฤƒtraลŸcu and Erik Demaine (2006) proved an ฮฉ(log n / log log n) cell-probe lower bound per operation, which is matched in practice by a segment tree in O(log n) time for both point updates and range minimum queries.

References

  1. Bender, M. A., & Farach-Colton, M. (2000). "The LCA problem revisited." In Proceedings of the 4th Latin American Symposium on Theoretical Informatics (LATIN 2000), Lecture Notes in Computer Science, vol. 1776, 88โ€“94. https://doi.org/10.1007/10719839-9
  2. Fischer, J., & Heun, V. (2011). "Space-efficient preprocessing schemes for range minimum queries on static arrays." SIAM Journal on Computing, 40(2), 465โ€“492. https://doi.org/10.1137/090779759
  3. Berkman, O., & Vishkin, U. (1993). "Recursive star-tree parallel data structure." SIAM Journal on Computing, 22(2), 221โ€“242. https://doi.org/10.1137/0222017
  4. Gabow, H. N., Bentley, J. L., & Tarjan, R. E. (1984). "Scaling and related techniques for geometry problems." In Proceedings of the Sixteenth Annual ACM Symposium on Theory of Computing (STOC '84), 135โ€“143. https://doi.org/10.1145/800057.808675
๐Ÿงฌ Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
๐Ÿ’ก Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision