Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

How many non-empty contiguous subarrays of [1, -1, 2, 0, 1, 3, -2, 1] have a sum equal to their length (number of elements)? Transform each element to a[i] − 1 and count zero-sum subarrays.

  1. A. 8
  2. B. 7
  3. C. 5
  4. D. 3

Correct answer: B. 7

Subtract 1 from every element: the condition becomes a zero-sum subarray, which is counted with prefix sums and a frequency map. Checking each element against 1 misses the longer subarrays.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic