Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS

Which of the following statements about the sliding window technique on arrays with NEGATIVE numbers is/are correct? I. A plain shrink-from-left window correctly finds the longest subarray with sum at most K. II. A prefix-sum structure (a monotone deque or a sorted map) can find the shortest subarray with sum at least K. III. Counting subarrays with sum exactly K can be done with a prefix-sum frequency map in O(n).

  1. A. I and II only
  2. B. I only
  3. C. I, II and III
  4. D. II and III only

Correct answer: D. II and III only

I is false: adding a negative element can reduce the sum, so shrinking from the left does not keep the window monotone. II and III are standard prefix-sum techniques that handle negative values.

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

Exams that ask this topic