Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

For each element of an array of distinct integers, the task is to count how many earlier elements are smaller. Which structure achieves O(n log n) total time?

  1. A. Fenwick tree (binary indexed tree) over compressed values
  2. B. Monotonic stack alone
  3. C. A single prefix-sum array
  4. D. Sorting the whole array once without indices

Correct answer: A. Fenwick tree (binary indexed tree) over compressed values

Process left to right, query the count of smaller values inserted so far, then insert the current value in a Fenwick tree over compressed ranks. A monotonic stack only finds the nearest smaller element, not the total count.

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

Exams that ask this topic