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?
- A. 26
- B. 30
- C. 20
- 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
- The Z-array of a string s has Z[i] = length of the longest common prefix of s and s[i..]. (Z[0] is taken as 0.) What is the sum of all Z…
- What is the length of the longest palindromic substring (contiguous) of xabcbaxyzzyxabcdcbay?
- Consider the congruence 46x ≡ 18 (mod 240). How many solutions x does it have in 0…239, and what is the smallest one?
- How many integers in 1…1000 are coprime to 360?
- A dynamic array has capacity 1 initially. An append to a full array doubles the capacity (copying all n current elements); after a…
- A 4-input comparator network applies, in order, the comparators (0,1), (2,3), (0,2), (1,3), (1,2), each putting the smaller value on the…