Data Structures & AlgorithmsModerate

In the array [2, 3, 1, 2, 4, 3] (all positive), what is the length of the shortest contiguous subarray whose sum is at least 7?

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

Correct answer: C. 2

For positive values the shortest qualifying window is found by growing the right end and shrinking the left while the sum stays ≥ S. The first window met in scan order need not be the shortest, and the strict > bound gives a different answer.

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

Exams that ask this topic