Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

What is the length of the longest contiguous subarray of [2, 1, 5, 1, 3, 2, 4, 1] whose sum is at most 9? All values are non-negative.

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

Correct answer: A. 4

With non-negative values the window sum only grows when extended, so a shrink-from-left two-pointer scan finds the longest window in O(n). The brute check over subarrays agrees with it on this input.

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

Exams that ask this topic