Data Structures & AlgorithmsHardGATE CS · DSA
How many contiguous subarrays of [3, 2, −3, 5, 2, −1, 0, 1, 2] have sum exactly 3? (Prefix sums with a hash map of prefix-sum frequencies.)
- A. 6
- B. 4
- C. 5
- D. 7
Correct answer: C. 5
Walk the array keeping prefix sum P and a map of how often each earlier prefix occurred (with P = 0 seeded once); add freq[P − 3] at every step. The total is 5. Forgetting the seeded 0 misses subarrays that start at index 0.
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
- How many pairs (i < j) of [6, 27, 29, 25, 10, 6, 25, 3, 7, 22] have a[i] + a[j] divisible by 6? (Count remainders mod 6 and pair r with 6…
- A queue is built from two stacks: enqueue pushes onto IN; dequeue pops from OUT, first moving every element of IN onto OUT only when OUT…
- LSD radix sort (base 10, stable sort on each digit, units digit first) is applied to [148, 261, 414, 335, 678, 773, 321]. What is the list…
- Items P, Q, R, S, T, U are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- How many contiguous subarrays of [7, 6, 8, 5, 9, 1, 1, 9, 1, 9] contain exactly 2 odd numbers? (Use exactly(k) = atMost(k) − atMost(k − 1).)
- Longest run of consecutive integers (in any order) using a hash set and starting only from numbers whose predecessor is absent. What is…