Data Structures & AlgorithmsModerateTCS NQT · Infosys

Array a = [1, 2, 3, 4, -1, 5, 0, 2]. How many contiguous subarrays have a sum of exactly 10?

  1. A. 2
  2. B. 3
  3. C. 1
  4. D. 4

Correct answer: A. 2

Prefix sums give every subarray sum as a difference of two prefixes, so counting pairs whose difference is 10 with a hash map counts all subarrays. Zero-valued elements create several subarrays with the same sum, which the count must keep.

More Coding Interview Problems questions

Exams that ask this topic