ProgrammingVery hard

To find the maximum over 10^5 queries of ranges in a static array, which approach is fast enough?

  1. A. Scan each range, about 10^10 operations
  2. B. A sparse table built in O(n log n), then each query in O(1)
  3. C. Sort the whole array before each query, about 10^10 operations
  4. D. Recompute the maximum by recursion over all ranges, about 10^15 operations

Correct answer: B. A sparse table built in O(n log n), then each query in O(1)

A sparse table makes each query O(1) after an O(n log n) build, which is about 2 x 10^6 steps. Scanning costs up to 10^10.

More C Programming questions

Exams that ask this topic