Data Structures & AlgorithmsModerateCodeforces Div2 C · GATE CS

A Fenwick tree t[1..6] (1-indexed, all zero at the start) is updated by add(i, v): set j = i, then while j ≤ n do { t[j] += v; j += j & −j }. The calls, in order, are add(4, -3), add(2, -1), add(6, -3), add(5, -2), add(4, -2), add(2, 2). What is the final value of t[6]?

  1. A. -3
  2. B. -5
  3. C. -9
  4. D. 0

Correct answer: B. -5

Each update touches exactly the nodes on the chain j, j + lowbit(j), ... up to n. Stopping early or updating only t[i] gives a different value; t[k] is generally not the point value a[k].

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic