Data Structures & AlgorithmsHardGATE CS · Product placement

You must answer many queries 'sum of A[l..r]' while also updating single elements, on an array of n elements. Which choice gives O(log n) for both operations?

  1. A. A Fenwick (binary indexed) tree
  2. B. A difference array
  3. C. Sorting the array once
  4. D. A plain prefix-sum array

Correct answer: A. A Fenwick (binary indexed) tree

A static prefix array answers queries in O(1) but needs O(n) to update; a Fenwick tree supports both point updates and prefix sums in O(log n).

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

Exams that ask this topic