Data Structures & AlgorithmsHardGATE CS · TCS NQT

For the positive array [2, 3, 1, 2, 4, 3] and target S = 7, what is the length of the shortest contiguous subarray whose sum is at least 7?

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

Correct answer: B. 2

Two pointers give the minimum: [4, 3] at the end has length 2 and sum 7, which beats the first window [2, 3, 1, 2] of length 4 that reaches 8. Taking the first valid window instead of the minimum is the trap.

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

Exams that ask this topic