Data Structures & AlgorithmsHardGATE CS · TCS NQT

Which technique gives O(1) time per query for 'sum of a[l..r]' on a static array after O(n) preprocessing?

  1. A. Prefix sums
  2. B. Segment tree with lazy propagation
  3. C. Sorting plus binary search
  4. D. Hash map of every subarray sum

Correct answer: A. Prefix sums

Prefix sums answer each range sum as prefix[r] - prefix[l-1] in constant time. A segment tree answers in O(log n), which is slower than necessary for a static array.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic