Data Structures & AlgorithmsHardTCS Digital · Infosys SP

What is the length of the shortest contiguous subarray of [1, 3, 5, 6, 3, 3, 7, 9, 4, 9] whose sum is at least 20? (All values are positive, so a shrinking sliding window works.)

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

Correct answer: B. 3

Expand right until the sum reaches 20, then shrink from the left while it stays ≥ 20, recording the length each time. The minimum length is 3.

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

Exams that ask this topic