Data Structures & AlgorithmsModerateGATE CS · Product placement
A prefix-sum array P of A (P[0] = 0, P[i] = A[0] + … + A[i − 1]) is built. Which expression gives the sum A[l] + … + A[r] (0-based, inclusive)?
- A. P[r] − P[l]
- B. P[r + 1] − P[l + 1]
- C. P[r + 1] − P[l]
- D. P[r] − P[l − 1]
Correct answer: C. P[r + 1] − P[l]
P[r + 1] includes A[0..r] and P[l] includes A[0..l − 1]; their difference is exactly A[l..r]. P[r] − P[l − 1] is the formula for a 1-based prefix array.
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
- A peak is an element larger than its neighbours (array ends count as −∞). Binary search runs on [16, 19, 40, 1, 18, 30, 54, 59, 45]: while…
- The median-of-medians (groups of 5) selection algorithm is used to choose a pivot. What does it guarantee?
- Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed? python def rotate(h, k): n, t = 1, h while t.nx…
- Over all left rotations of [7, 7, 1, 1, 3, 6], what is the maximum value of Σ i·a[i] (0-based)? (Use R(j) = R(j − 1) + sum − n·a[n − j] to…
- Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is…
- A min-stack keeps a second stack of running minimums. What is printed? python st, mn, out = [], [], [] for op in [7, 7, "pop", 3, "pop"…