Data Structures19 sections · 729 units
Open in Course

RMQ Solution

Implement your solution

Implement Sparse Table for range minimum queries.

Implementation details:

  • K=log2n+1K = \lfloor \log_2 n \rfloor + 1 is the number of power-of-2 lengths needed
  • The inner loop bound n - (1 << j) + 1 ensures we don't exceed array bounds
  • Precomputed LOG array avoids expensive log calls Testing:
  • Single element range
  • Entire array range
  • Random ranges
  • Verify against brute force for small inputs For 10610^6 queries on 10510^5 elements, Sparse Table answers each query in O(1)O(1) vs. O(logn)O(\log n) for a segment tree.

Time: O(1)O(1). Space: O(nlogn)O(n \log n).