Range Minimum Query
RMQ algorithm
computational algorithms
data structures
query optimization

Range Minimum Query On, O1 approach Last steps

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Overview

The Range Minimum Query (RMQ) problem is a fundamental problem in computer science and is often encountered in many applications, such as finding the minimum value in a segment of an array or preprocessing data for efficient interval queries. The challenge is to create data structures and algorithms that enable quick lookup times while balancing the preprocessing complexity.

One popular solution for addressing the RMQ problem involves preprocessing the data using an <O(n), O(1)> approach, where the preprocessing time is linear, and each query can be resolved in constant time.

Understanding the <O(n), O(1)> Strategy

The Sparse Table Method

A prominent method achieving &lt;O(n), O(1)> complexity is the Sparse Table method. This method involves preprocessing an input array to facilitate fast minimum range queries.

Preprocessing Steps

  1. Initialization: • Given an array A of size n , create a table st[n][log(n)] . The st[i][j] entry holds the minimum value over the range starting at i with length 2^j .
  2. Base Case (k = 0): • For each element i from 0 to n-1 , set st[i][0] = A[i] . This corresponds to intervals of length 1 .
  3. Filling the Sparse Table: • For each j > 0 , and for each starting index i , calculate: st[i][j]=min(st[i][j1],st[i+2j1][j1])st[i][j] = \text{min}(st[i][j-1], st[i + 2^{j-1}][j-1]) • This recurrence relation combines two adjacent intervals of length 2^\&#123;j-1\&#125; to form intervals of length 2^j .
  4. Time Complexity: • This preprocessing requires consideration of all intervals of all powers of two, for every starting point, leading to a time complexity of O(nlogn)O(n \log n). However, with further optimizations, such as an exploration of only necessary intervals, can bring the construction time down to linear in certain scenarios.

Query Phase

Once the table is built, each RMQ can be answered in constant time.

  1. Determine the Length: • For a query range [L, R] , determine the largest power of two that fits within the range, say k = \lfloor \log_2(R - L + 1) \rfloor .
  2. Query the Sparse Table: • Compute the minimum using two overlapping intervals derived from the Sparse Table: RMQ(L,R)=min(st[L][k],st[R2k+1][k])\text{RMQ}(L, R) = \text{min}(st[L][k], st[R - 2^k + 1][k])

This guarantees that the result serves up the correct minimum element for the specified range in constant time.

Applications of RMQ

The RMQ problem and its solutions via methods like the Sparse Table are directly applicable to various fields:

Text Processing: Identifying the longest repeated substring efficiently. • Dynamic Programming: Solving problems like the longest increasing subsequence. • Computational Geometry: Managing objects in a spatial database via coordinate ranges.

Example

Consider an array A = [2, 4, 3, 1, 6, 7, 8, 9, 1, 7] . For n = 10 , we can build a Sparse Table to efficiently resolve queries.

Sparse Table Construction

  1. Base Case:
    • Compute st[i][j] for further j based on recursion as outlined. • RMQ(2, 5) using k=log2(4)=2k = \lfloor \log_2(4) \rfloor = 2. • Minimum of intervals from Sparse Table: min(st[2][2],st[5][2])\text{min}(st[2][2], st[5][2]).

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.