Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA

What is the length of the longest contiguous subarray of [2, 3, 1, 2, 4, 3] whose sum is exactly 7?

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

Correct answer: C. 3

Store the first index of each prefix sum and look for prefix minus target. For positive values the sliding window also works, and both give the same length.

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

Exams that ask this topic