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?
- A. A Fenwick (binary indexed) tree
- B. A difference array
- C. Sorting the array once
- 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).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- An array of 8 integers holds values 1..7 with exactly one value appearing twice: [6, 5, 7, 4, 3, 2, 1, 1]. Treating i → a[i] as a linked…
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "aabaabbbabab", "bab" over = sum(1 for i in…
- Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is…
- A fixed window of length 3 slides over s = "baacbbbcbcabab" with a character-count array. At how many starting positions is the window an…
- Keys are inserted into a hash table of size 10 with h(k) = k mod 10 and quadratic probing (h(k) + i²) mod 10. Which statement is correct?
- What does this loop-counting code print? (Python) python n, c = 64, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)