Data Structures & AlgorithmsHard

What is the length of the shortest contiguous subarray of 1 4 4 2 2 1 3 whose sum is at least 9?

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

Correct answer: B. 3

All values are positive, so a two-pointer window that grows right and shrinks left while the sum stays at least S finds the shortest qualifying block. Using a strict bound or an off-by-one length changes the result.

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

Exams that ask this topic