Data Structures & AlgorithmsHard

What is the length of the longest contiguous subarray of 2 0 1 3 1 1 2 0 in which the sum of (element − 1) over the subarray equals 0?

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

Correct answer: D. 4

Subtracting one from each element turns the condition into a zero-sum subarray, and the longest one comes from first-occurrence prefix sums. Comparing the sum to the length gives the same test only when the subtraction is applied to every element, which the question specifies.

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

Exams that ask this topic