Data Structures & AlgorithmsVery hard

What is the length of the shortest contiguous subarray of 1 4 2 7 3 5 6 1 whose sum is at least 14?

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

Correct answer: B. 3

Two pointers grow the right end and shrink the left end while the sum stays at least S, giving the minimum length in linear time for positive values. A strict bound or an off-by-one target changes the answer.

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

Exams that ask this topic