Data Structures & AlgorithmsModerateTCS NQT · Infosys

Array a = [2, -2, 3, -3, 4, -4, 1]. How many DISTINCT values appear among the prefix sums P[0], P[1], ..., P[7] (including P[0] = 0)?

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

Correct answer: B. 5

Compute the eight prefix values 0, 2, 0, 3, 0, 4, 0, 1. Several prefixes repeat the value 0, so the number of distinct values is smaller than the number of prefixes.

More Coding Interview Problems questions

Exams that ask this topic