GATE Computer ScienceModerateGATE CS (PYQ pattern)

A Fenwick tree (binary indexed tree) over positions 1–100 performs update(i) by visiting i, then i + (i & −i), and so on while the index is ≤ 100. For how many starting positions i in 1…100 does update(i) visit exactly 3 nodes?

  1. A. 26
  2. B. 30
  3. C. 20
  4. D. 13

Correct answer: A. 26

Simulating all 100 starts gives the distribution of visited-node counts 1:3, 2:13, 3:26, 4:30, 5:20, 6:7, 7:1 (e.g. i = 16 visits 16, 32, 64; i = 1 visits 1, 2, 4, …, 64). Exactly 3 nodes occur for 26 values of i; 30 is the count for 4 nodes.

More Algorithms questions

Exams that ask this topic