Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many contiguous subarrays of [1, 2, -3, 3, -3, 2, 1] have sum exactly zero?
- A. 6
- B. 7
- C. 8
- D. 10
Correct answer: A. 6
A zero-sum subarray is a pair of equal prefix sums, so count repeats among the prefix sums (with the empty prefix 0) using a hash map.
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 index pairs i < j in [1, 3, 5, 7, 9, 4] have |a[i] - a[j]| = 2?
- How many distinct values appear among the XORs of all non-empty contiguous subarrays of [1, 2, 3]?
- How many windows of length 3 in the string abaaabaa are anagrams of aab (same letters with the same counts)?
- What is the length of the longest substring of aabaaccaa that contains at most two letters 'a'?
- How many palindromic substrings (counted by position) does the string abbaabba contain?
- How many distinct palindromic substrings does the string aabaa contain?