Build a sparse table for O(1) range minimum queries. Visualise every level of the 2D precomputation table, run live queries, and copy the C++ template.
Paste a list of integers and the Sparse Table Builder constructs the full 2D precomputation table that makes range-minimum queries answerable in O(1) time. The table has ⌊log₂ n⌋ + 1 levels; level k stores the minimum of every window of length 2k starting at each index. Once that table exists, any query over a[l..r] resolves by reading two cells, one anchored at l and one ending at r, and returning the smaller of the two because the windows overlap and a minimum is idempotent.
RMQ appears throughout competitive programming wherever you need the cheapest or most powerful element in a moving window: lowest common ancestor on a tree (after Euler-tour flattening), static range queries on a frequency table, the denominator in a sliding-window ratio problem, and anywhere a Segment Tree would work but its O(log n) query is a bottleneck. Sparse Table trades update capability for raw query speed. The structure is read-only after construction, which makes it the right call when the array is fixed and queries are dense.
The lead sample is 3 6 2 1 8 4 7 5 9, nine elements. Level 0 is the array itself. Level 1 stores window-of-2 minima: min(3,6)=3, min(6,2)=2, min(2,1)=1, and so on. Level 2 stores window-of-4 minima: min(3,6,2,1)=1 at index 0, min(6,2,1,8)=1 at index 1, etc. A query over [1,4] (values 6 2 1 8) uses k=2 (the largest power of 2 that fits 4 elements): sp[2][1] = min(6,2,1,8) = 1. Change the query to [0,2] and the tool returns 2, the minimum of 3 6 2.
Building the table takes O(n log n) time and space. Querying any range [l, r] takes O(1): compute k = ⌊log₂(r − l + 1)⌋, then return min(sp[k][l], sp[k][r − 2k + 1]). The overlapping windows are valid because minimum is idempotent: counting an element twice does not change the result. Arrays up to 16 elements are accepted here; the generated C++ template scales to MAXN = 100001 and uses GCC's built-in __lg(x) for the floor-log₂ computation, which is a single instruction on x86.
LOG = ⌊log₂ n⌋ + 1 at compile time. If you resize MAXN without updating LOG, level k = LOG will silently read out of bounds. Always derive both constants from the same value.a[0..8] — 9 elements
Sparse Table — sp[k][i] = min of a[i .. i + 2k − 1]
4 levels (k = 0 … 3) | Cells outside the valid window are shown as —
| k \ i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | window |
|---|---|---|---|---|---|---|---|---|---|---|
| k=0 | 3 | 6 | 2 | 1 | 8 | 4 | 7 | 5 | 9 | 20 = 1 |
| k=1 | 3 | 2 | 1 | 1 | 4 | 4 | 5 | 5 | — | 21 = 2 |
| k=2 | 1 | 1 | 1 | 1 | 4 | 4 | — | — | — | 22 = 4 |
| k=3 | 1 | 1 | — | — | — | — | — | — | — | 23 = 8 |
Query Playground — O(1) Range Minimum Query
C++ Sparse Table Template
// Sparse Table (RMQ) — generated by UtilityToolsLab
// Array: {3, 6, 2, 1, 8, 4, 7, 5, 9} | N = 9
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
const int LOG = 4; // floor(log2(n)) + 1
int sp[LOG][MAXN];
void build(int* arr, int n) {
for (int i = 0; i < n; i++) sp[0][i] = arr[i];
for (int k = 1; (1 << k) <= n; k++)
for (int i = 0; i + (1 << k) - 1 < n; i++)
sp[k][i] = min(sp[k-1][i],
sp[k-1][i + (1 << (k-1))]);
}
// O(1) query: minimum of a[l..r] (0-indexed, inclusive)
int query(int l, int r) {
int k = __lg(r - l + 1); // __lg(x) = floor(log2(x)) in GCC
return min(sp[k][l], sp[k][r - (1 << k) + 1]);
}
int main() {
int arr[] = {3, 6, 2, 1, 8, 4, 7, 5, 9};
int n = 9;
build(arr, n);
// Example queries
cout << query(0, 8) << "\n"; // min of entire array
cout << query(1, 3) << "\n"; // min of [1,3]
}