Data Structures & AlgorithmsHardTCS Digital · Infosys SP
How many contiguous subarrays of [−2, 0, 5, −1, 4, 3, −1, −2, −3] have sum exactly 4? (Prefix sums with a hash map of prefix-sum frequencies.)
- A. 4
- B. 5
- C. 6
- D. 3
Correct answer: A. 4
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 − 4] at every step. The total is 4. 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
- For s = "cdbccdddbaddca", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by…
- For s = "bcadbdaccdddbd", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by…
- How many contiguous subarrays of [1, 3, 8, 1, 3, 6, 0, 0, 9] have a sum divisible by 3? (Count equal prefix-sum remainders.)
- Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only? python a = [23…
- What does this loop-counting code print? (Python) python n, c = 15, 0 for i in range(1, n + 1): j = 1 while j <= n: c += 1 j += i print(c)
- Longest run of consecutive integers (in any order) using a hash set and starting only from numbers whose predecessor is absent. What is…